cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cypher to group data

Capture d’écran du 2023-01-24 15-45-35.png

Hello Everyone,

I really need your help with a cypher code. I am working on a project and I a have the schema below.

I want for every element in the "Network_Name" column and for each  Datetime (distinct selection according to the Network_Name and Datetime) to count the number of user and return those three columns ("Network_Name", "Datetime" and user count).

Any help please

I hope I was clear enough

Thank in advance

 

1 ACCEPTED SOLUTION

Assuming a data model as follows:

Screen Shot 2023-01-24 at 12.07.23 PM.png

Then, the following query should work:

match(n:Network)<-[:BELONGS_TO]-(u:User)
return n.network_name as network_name, n.datetime as datetime, count(u) as user_count

 The key point is in the 'with' statement, where the statement groups the rows by 'network_name' and 'datatime' of the network node, then counts the number of users for each distinct group. This is how the aggregation functions works, i.e., the rows are grouped by the distinct values of the variables provided on the 'with' or 'return' statement, whenever an aggregate functions exists. 

View solution in original post

3 REPLIES 3

You forgot to provide the schema/data model.

Assuming a data model as follows:

Screen Shot 2023-01-24 at 12.07.23 PM.png

Then, the following query should work:

match(n:Network)<-[:BELONGS_TO]-(u:User)
return n.network_name as network_name, n.datetime as datetime, count(u) as user_count

 The key point is in the 'with' statement, where the statement groups the rows by 'network_name' and 'datatime' of the network node, then counts the number of users for each distinct group. This is how the aggregation functions works, i.e., the rows are grouped by the distinct values of the variables provided on the 'with' or 'return' statement, whenever an aggregate functions exists. 

Hello 

Sorry I forget the schema but you guess it right.

That work perfectly.

Thank you very much