user2965003
user2965003

Reputation: 334

How to find nodes related to X and count how many of them are related to Y

My db contains Persons and Events as Nodes. A Person can :KNOW another Person, :CREATE or :ATTEND an Event.

Starting with the querying Person, i want to recieve a List of all Events a Person I know created, the number of Persons who attend the event (if there are any), and how many Persons I know attend this event (if there are any).

So my results should look like this:

Event     |     count(Attending)  |     count(FriendswhoAttend)

Event A                9                                   3
Event B                3                                   0
Event C                0                                   0

I get event and count(Attending) with

START me=node(1)  
MATCH (me)-[:KNOWS]-(creator)-[:CREATED]-(event), (Attender)-[?:ATTEND]-(event)  
RETURN event, count(Attender)

[?:ATTEND] is optional because it would eliminate events without attenders otherwise.

However, I cant figure out how to query for how many Persons I know attend the event (aka how many friends will be there).

The Problem is, using Match eliminates the found event IF there aren´t any Attenders I know:

WITH n, event  
MATCH (n)-[:KNOWS]-(friend)-[:ATTEND]-(event)

Making on of these relationships optional doesn´t help as it either returns all attenders or all friends.

Splitting the Querys would give me several lists of the same events.

How can I have Events with and without Attenders in one Resultset with friends who are attending?

Upvotes: 1

Views: 100

Answers (1)

Lisa Li
Lisa Li

Reputation: 2592

How about add a filter to the collection of "Attender" to get the friend attenders like this,

START me=node(1)  
MATCH (me)-[:KNOWS]-(creator)-[:CREATED]-(event), (Attender)-[?:ATTEND]-(event)
With event as e, collect(Attender) as attenders, me
Return e, length(attenders), length(filter(x in attenders where (me)-[:KNOWS]->x))

Upvotes: 1

Related Questions