英文:
Not able to use COUNT properly in cypher query
问题
已经有一个查询写好了,可以返回一些结果。
MATCH (n:Prop)
WHERE toLower(n.Type) = toLower("abc")
WITH n.Id AS id, n
MATCH (new:op)
WHERE new.Id STARTS WITH id
WITH new, n
MATCH (o:`here`)-[:Property]->(new)
WHERE (o:Obj)
WITH Count(DISTINCT o) AS languages, n, new
RETURN count(n)
这个查询返回了我期望的数量 6。
但是因为我想要同时获取 id,所以我修改了查询如下:
MATCH (n:Prop)
WHERE toLower(n.Type) = toLower("abc")
WITH n.Id AS id, n
MATCH (new:op)
WHERE new.Id STARTS WITH id
WITH new, n
MATCH (o:`here`)-[:Property]->(new)
WHERE (o:Obj)
WITH Count(DISTINCT o) AS languages, n, new
RETURN count(n), id(n)
这个查询的问题是,它返回了每个 id 的计数,但我希望每行只返回总计数,也就是 6。
我尝试了很多不同的方法,但无法得到我想要的结果。
英文:
I have a query already written which returns some result
MATCH(n:Prop)
WHERE toLower(n.Type)=toLower("abc")
WITH n.Id AS id, n
MATCH (new:op)
WHERE new.Id STARTS WITH id
WITH new,n
MATCH (o:`here`)-[:Property]->(new)
WHERE (o:Obj)
WITH Count(DISTINCT o) AS languages,n,new
return count(n)
This returns me count 6 which is expected
But since I want the id as well I change the query as
MATCH(n:Prop)
WHERE toLower(n.Type)=toLower("abc")
WITH n.Id AS id, n
MATCH (new:op)
WHERE new.Id STARTS WITH id
WITH new,n
MATCH (o:`here`)-[:Property]->(new)
WHERE (o:Obj)
WITH Count(DISTINCT o) AS languages,n,new
return count(n), id(n)
The problem with this is that I get the count as one with each id but I want the totalCount which was 6 only with each row
I have tried many different things but couldnt get what I want.
答案1
得分: 1
以下是翻译好的部分:
你需要收集和展开所有节点,就像这样:
MATCH(n:Prop)
WHERE toLower(n.Type)=toLower("abc")
WITH n.Id AS id, n
MATCH (new:op)
WHERE new.Id STARTS WITH id
WITH new,n
MATCH (o:`here`)-[:Property]->(new)
WHERE (o:Obj)
WITH Count(DISTINCT o) AS languages, n, new
WITH count(n) as nodesCount, COLLECT(n.Id) as ids
UNWIND ids as nodeId
RETURN nodesCount, nodeId
英文:
You need to collect and unwind all the nodes, like this:
MATCH(n:Prop)
WHERE toLower(n.Type)=toLower("abc")
WITH n.Id AS id, n
MATCH (new:op)
WHERE new.Id STARTS WITH id
WITH new,n
MATCH (o:`here`)-[:Property]->(new)
WHERE (o:Obj)
WITH Count(DISTINCT o) AS languages, n, new
WITH count(n) as nodesCount, COLLECT(n.Id) as ids
UNWIND ids as nodeId
RETURN nodesCount, nodeId
答案2
得分: 0
交换返回字段的位置。这样做的原因是Cypher执行隐式聚合,因此会按照列表中的编写顺序对每个字段进行聚合。在您的示例中,它首先会按count(n)
进行分组,然后再按id(n)
进行分组。我认为您希望根据每个id的计数进行分组。更新后的Cypher如下所示:
MATCH (n:Prop)
WHERE toLower(n.Type) = toLower("abc")
WITH n.Id AS id, n
MATCH (new:op)
WHERE new.Id STARTS WITH id
WITH new, n
MATCH (o:`here`)-[:Property]->(new)
WHERE (o:Obj)
WITH Count(DISTINCT o) AS languages, n, new
RETURN count(n), id(n)
英文:
Swap the places of the fields in your return. The reason for that is that Cypher does implicit aggregation, so it will aggregate on each field in the list in written order. In your example, it will group first on count(n)
, then on id(n)
. I think you want to group on the count for each id. Updated Cypher is below.
MATCH(n:Prop)
WHERE toLower(n.Type)=toLower("abc")
WITH n.Id AS id, n
MATCH (new:op)
WHERE new.Id STARTS WITH id
WITH new,n
MATCH (o:`here`)-[:Property]->(new)
WHERE (o:Obj)
WITH Count(DISTINCT o) AS languages,n,new
return id(n), count(n)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论