Search code examples
neo4jcypher

Get count of two column group on neo4j


Given a table of Packages like

Name Version
Pack1 1.0.0
Pack1 1.0.1
Pack2 2.0.0

I want to return a list of those packages with more than one version In SQL it would be something like

 SELECT Name from Package
 GROUP BY Name
 having count(Version) > 1 

how would I do something like this on Cypher (with or without apoc)


Solution

  • This is the equivalent of group by in sql to neo4j.

    Sql:
        SELECT Name from Package
         GROUP BY Name
         having count(Version) > 1 
    
    
    Neo4j:
        MATCH (p:Package)
        WITH p.name as name, count(p.version) as cnt WHERE cnt > 1
        RETURN name
    
    Result:
    ╒═══════╕
    │"name" │
    ╞═══════╡
    │"Pack1"│
    └───────┘