[英]Finding nodes which are connected to at least 2 same nodes through 2 relationships but with the intermediary one being different
I would like to find out all the couples of Persons and Accidents in the following case:我想找出以下案例中的所有人员和事故夫妇:
I always have the following relationships: Person --> Car -- > Accident我总是有以下关系:人 --> 车 --> 事故
I made a simplified and reproducible example:我做了一个简化且可重现的例子:
CREATE
(p1:Person {name: 'Paul'})-[:DRIVES]->(c1:Car {name: 'Car A'}),
(p2:Person {name: 'John'})-[:DRIVES]->(c2:Car {name: 'Car B'}),
(p3:Person {name: 'Mike'})-[:DRIVES]->(c3:Car {name: 'Car C'}),
(p4:Person {name: 'Joe'})-[:OCCUPANT]->(c1),
(p4)-[:OCCUPANT]->(c3),
(c1)-[:IS_DOER]->(a1: Accident {name: 'Crash 1'}),
(c2)-[:IS_VICTIM]->(a1),
(c2)-[:IS_DOER]->(a2: Accident {name: 'Crash 2'}),
(c3)-[:IS_VICTIM]->(a2) ;
And here is what the graph looks like:这是图表的样子:
In this example, I would like to return the couples ("Joe", "John") and ("Crash 1", "Crash 2"), because Joe and John are both involved into at least 2 same accidents while being in different cars.在这个例子中,我想返回这对夫妇 ("Joe", "John") 和 ("Crash 1", "Crash 2"),因为 Joe 和 John 都卷入了至少 2 起相同的事故,同时处于不同的汽车。
Thank you for your help.感谢您的帮助。
Actually I think I found the answer:其实我想我找到了答案:
MATCH
(p1:Person)-[r1]->(c1:Car)-[r2]->(a:Accident)<-[r3]-(c2:Car)<-[r4]-(p2:Person)
WHERE c1<>c2
WITH p1, p2, COUNT(DISTINCT a) AS count_accident
WHERE count_accident>= 2
RETURN p1, p2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.