[英]Neo4j Cypher: Group nodes by the relation to another node
Having a graph like post-->category
, how can I get one post per category? 有一个像post-->category
这样的图表,我怎样才能为每个类别发一个帖子?
ie: 即:
Having
Post A1 --> Category A
Post A2 --> Category A
Post B1 --> Category B
Post B2 --> Category B
Post B3 --> Category B
Post C1 --> Category C
I should get Post A2, Post B1, Post C1.
I don't mind what post I get for a given category, just to get one for each category. 我不介意给出一个给定类别的帖子,只为每个类别获得一个。
Thanks! 谢谢!
To pick a post per category by random: 要随机选择每个类别的帖子:
MATCH (p:Post)-[:HAS_CATEGORY]->(c:Category)
WITH c, collect(p) as posts
RETURN c, posts[toInt(rand()*length(posts))]
We're using the collect
aggregation function per category and in the return we pick one entry by random. 我们使用每个类别的collect
聚合函数,并在返回时我们随机选择一个条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.