Neo4j Cypher Group By

huangapple go评论58阅读模式
英文:

Neo4j Cypher Group By

问题

如何继续查询以返回cs和每个不同s的计数?

英文:

I have the following Cypher query:

MATCH (c:Criterion {id: 24}) 
OPTIONAL MATCH (c)<-[:HAS_VOTE_ON]-(d:Decision)-[:HAS_VOTE_ON]->(s:Skill) 
WHERE s.id <> c.id WITH c, s

How to continue the query in order to return for c: s and count of each distinct s ?

答案1

得分: 1

你需要同时收集并计数s,然后如果要继续处理每个单独的s节点,就需要将收集的内容解析回单独的行。

MATCH (c:Criterion {id: 24}) 
OPTIONAL MATCH (c)<-[:HAS_VOTE_ON]-(d:Decision)-[:HAS_VOTE_ON]->(s:Skill) 
WHERE s.id <> c.id 
WITH c, collect(DISTINCT s) as skills, count(DISTINCT s) as count
UNWIND skills as s
...
英文:

You would need to collect and count s at the same time, then UNWIND the collection back into individual rows if you want to continue with each individual s node.

MATCH (c:Criterion {id: 24}) 
OPTIONAL MATCH (c)&lt;-[:HAS_VOTE_ON]-(d:Decision)-[:HAS_VOTE_ON]-&gt;(s:Skill) 
WHERE s.id &lt;&gt; c.id 
WITH c, collect(DISTINCT s) as skills, count(DISTINCT s) as count
UNWIND skills as s
...

答案2

得分: 1

If you just want to return each distinct s and its count:

匹配(c:标准{ id:24 })& lt; -[:HAS_VOTE_ON]-(:决策)-[:HAS_VOTE_ON]-& gt;(s:技能)
其中 s.id & lt; & gt; c.id
返回 s,COUNT(*) AS cnt

英文:

If you just want to return each distinct s and its count:

MATCH (c:Criterion {id: 24})&lt;-[:HAS_VOTE_ON]-(:Decision)-[:HAS_VOTE_ON]-&gt;(s:Skill) 
WHERE s.id &lt;&gt; c.id
RETURN s, COUNT(*) AS cnt

huangapple
  • 本文由 发表于 2023年6月29日 05:50:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定