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.

Count in grouped nodes

I'm new to Cypher and I've been struggling with this particular query.

I have Person and Workshop nodes. A person is from a country.

I need a query that groups people based on their country, counts how many people is from each country and counts the relationships between countries.

Let's say Workshop A is related to Person 1 (from US) and Person 2 (from Canada). Also, Workshop B is related to Person 3 (from Brazil), Person 4 (from US) and Person 1.

Then, the expected result would be:

  • US node with 2 as the count property value
  • Canada node with 1 as the count property value
  • Brazil node with 1 as the count property value
  • Relationship between US and Brazil with 2 as the count property value (Person 1, Person 4, and Person 3 are related to Workshop B)
  • Relationship between US and Canada with 1 as the count property value (Person 1, Person 2 are related to Workshop A)
  • Relationship between US and US (self-relationship) with 1 as the count property value (Person 1, and Person 4 are related to Workshop B)

How can I get that data? I've tried just grouping but I couldn't find a way of getting the count of people grouped in each node.

2 REPLIES 2

accounts
Node Clone

given this schema that i understood from your question

(Workshop)-[RELATED]->(Person)-[FROM]->(Country)

this query seems to return what you're asking for

match (c1:Country)-[:FROM]-(p1:Person)-[:RELATED]-(w:Workshop)-[:RELATED]-(p2:Person)-[:FROM]-(c2:Country)
where c1<>c2 and p1<>p2
with c1, size (collect (distinct p1)) as citizenCount, size(collect(distinct c2)) as countryRelationCount
return c1.name, citizenCount, countryRelationCount

returns

"c1.name"│"citizenCount"│"countryRelationCount"│
╞═════════╪══════════════╪══════════════════════╡
│"Brazil" │1             │1                     │
├─────────┼──────────────┼──────────────────────┤
│"Canada" │1             │1                     │
├─────────┼──────────────┼──────────────────────┤
│"US"     │2             │2                     │

Thanks for replying, @accounts!

Sorry if my question wasn't clear enough.

The schema is (Person)-[RELATED]-(Workshop), and the Person nodes have a property country.

Also, what I need is to know the relationship between pairs of countries. So the result should be something like this:

|    Country 1              |    Country 2                |Count |
╞═══════════════════════════╪═════════════════════════════╪══════╡
|{ name: "US", count: 2 }   | { name: "US", count: 2 }    | 1    |
├───────────────────────────┼─────────────────────────────┼──────┤
|{ name: "US", count: 2 }   | { name: "Brazil", count: 1 }| 2    |
├───────────────────────────┼─────────────────────────────┼──────┤
|{ name: "US", count: 2 }   | { name: "Canada", count: 1 }| 1    |
├───────────────────────────┼─────────────────────────────┼──────┤