neo4j 如何得到一个简单的返回而不需要相同的输出double(对于Cypher查询)?

j2cgzkjk  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(145)

我目前得到的查询结果是:

我正在寻找可提供此结果的查询:

我使用了以下查询,但得到了不希望的结果:

MATCH q = (r:Representative {name: "Graaf"})-[job:FACILITATED]->(b)-[x: jaar_sinds_registratie_coördinator]->(a)
WHERE a.year < 13 AND NOT b:VuurpijlMatch
WITH DISTINCT count(labels(b)) AS countlabel1, labels(b) as labelname1
WITH DISTINCT labelname1, countlabel1

MATCH q = (r:Representative {name: "Graaf"})-[job:FACILITATED]->(b)-[x: jaar_sinds_registratie_coördinator]->(a)
WHERE a.year > 24 AND NOT b:VuurpijlMatch
WITH DISTINCT count(labels(b)) AS countlabel2, labels(b) as labelname2, countlabel1
RETURN DISTINCT labelname2, countlabel2, countlabel1

谁能给我一个答案/告诉我在这种情况下我忽略了什么?
谢谢你!

yeotifhr

yeotifhr1#

问题出在此汇总运算WITH DISTINCT count(labels(b)) AS countlabel2, labels(b) as labelname2, countlabel1。当您执行此运算时,countlabel2会针对labelname2countlabel1的每个不同组合进行计算。这就是为何会出现重复记录的原因。作为参考,您的countlabel2会针对下列组合进行计算:

labelname2    countlabel1

NetworkMatch  136
NetworkMatch  5
SettledMatch  136
SettledMatch  5

这可以通过在WITH中提取labelname1来解决,这样聚合的组合就变成了:

labelname2    labelname1    countlabel1

NetworkMatch  NetworkMatch  136
NetworkMatch  SettledMatch  5
SettledMatch  NetworkMatch  136
SettledMatch  SettledMatch  5

在这之后,我们可以简单地返回行,其中labelname1等于labelname2。因此,您的查询现在变成这样:

MATCH q = (r:Representative {name: "Graaf"})-[job:FACILITATED]->(b)-[x: jaar_sinds_registratie_coördinator]->(a)
WHERE a.year < 13 AND NOT b:VuurpijlMatch
WITH DISTINCT count(labels(b)) AS countlabel1, labels(b) as labelname1
WITH DISTINCT labelname1, countlabel1

MATCH q = (r:Representative {name: "Graaf"})-[job:FACILITATED]->(b)-[x: jaar_sinds_registratie_coördinator]->(a)
WHERE a.year > 24 AND NOT b:VuurpijlMatch
WITH DISTINCT count(labels(b)) AS countlabel2, labels(b) as labelname2, countlabel1, labelname1
WITH labelname2, countlabel2, countlabel1 WHERE labelname2 = labelname1
RETURN labelname2, countlabel2, countlabel1

相关问题