Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
09-13-2020 04:39 PM
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:
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.
09-21-2020 01:19 PM
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 │
09-21-2020 04:31 PM
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 |
├───────────────────────────┼─────────────────────────────┼──────┤
All the sessions of the conference are now available online