如何在 Neo4j 中按不同的节点 属性 对节点进行分组并按不同的节点排序?
How do I group nodes by a different node property and order by a different one in Neo4j?
我正在使用 crimes in Boston 数据集,将每起犯罪作为 Neo4j 中的节点。我想查询并显示每年犯下最多的罪行,得到这样的结果:
Year
offense_code_group
count(offense_code_group)
2015
Aggravated Assault
5827
2016
Larceny From Motor Vehicle
11534
2017
Verbal Disputes
12049
2018
Investigate Person
8724
注意:结果只是我希望它如何分组和查看的示例,而不是查询数据集后得到的实际结果。
但这是迄今为止我能做到的最好的事情:
query and output in neo4j desktop
我知道 Neo4j 中没有 GROUP BY 子句,我已经尝试使用 collect(),但我无法让它工作。
这样的事情怎么样:
MATCH (c:Crime)
WITH c.year AS year, c.offense_code_group AS code, count(c.offense_code_group) AS count
ORDER BY year, count DESC
WITH year, COLLECT(code) AS codes, COLLECT(count) AS counts
RETURN year, codes[0], counts[0]
我正在使用 crimes in Boston 数据集,将每起犯罪作为 Neo4j 中的节点。我想查询并显示每年犯下最多的罪行,得到这样的结果:
Year | offense_code_group | count(offense_code_group) |
---|---|---|
2015 | Aggravated Assault | 5827 |
2016 | Larceny From Motor Vehicle | 11534 |
2017 | Verbal Disputes | 12049 |
2018 | Investigate Person | 8724 |
注意:结果只是我希望它如何分组和查看的示例,而不是查询数据集后得到的实际结果。
但这是迄今为止我能做到的最好的事情: query and output in neo4j desktop
我知道 Neo4j 中没有 GROUP BY 子句,我已经尝试使用 collect(),但我无法让它工作。
这样的事情怎么样:
MATCH (c:Crime)
WITH c.year AS year, c.offense_code_group AS code, count(c.offense_code_group) AS count
ORDER BY year, count DESC
WITH year, COLLECT(code) AS codes, COLLECT(count) AS counts
RETURN year, codes[0], counts[0]