Maham Azmat
Maham Azmat

Reputation: 21

Neo4j query for taking max of a column based on group by of another column

I want to convert the below SQL query to Neo4j query:

select max(col1) from screens GROUP BY col2

I want to get a group of similar col2 and then extract max col2 from it. I am unable to find group by in neo4j, some of the posts are using Count(*) instead of group by in neo4j, but that does not fits in my problem. The above query is working fine in SQL but I need it for neo4j.

Any idea how it can be done? Thanks

Upvotes: 2

Views: 285

Answers (1)

jose_bacoy
jose_bacoy

Reputation: 12684

The SQL equivalent of Max/Group By in cypher is doing a collect, unwind then get the max value.

 MATCH (n: Screens)
 WITH collect(n.col1) as coll_n1, n.col2 as col2
 UNWIND coll_n1 as col1
 WITH col2, max(col1) as max_col1  
 RETURN col2, max_col1

is equivalent to

 select col2, max(col1) 
 from screens 
 GROUP BY col2

References:

UNWIND: WITH:

Upvotes: 1

Related Questions