Neo4j比较不同查询的总和并显示结果

时间:2018-09-20 16:37:22

标签: neo4j nosql neo4j-apoc

是否可以比较不同查询的结果?可以将以下查询与所需结果的返回值一起写在单个查询中吗?

Query1 :从不属于其区域的国家给出的所有点返回国家和SUM。

MATCH (c:Country)
with c
MATCH (c)-[vFrom:vote_from]->(vP:vote_points)-[vFor:vote_for]->(c2:Country)
with c,c2,vP
where not c.region=c2.region
return c2.name,sum(toInteger(vP.points))

查询1返回的示例:

"Monaco"    11
"Montenegro"    34
"France"    359
"Cyprus"    600
"Romania"   837

Query2 :从属于其区域的国家给出的所有点中返回国家和SUM。

MATCH (c:Country)
with c
MATCH (c)-[vFrom:vote_from]->(vP:vote_points)-[vFor:vote_for]->(c2:Country)
with c,c2,vP
where  c.region=c2.region
return c2.name,c.name,sum(toInteger(vP.points))

查询2返回的示例:

"Monaco"    35
"Montenegro"    66
"France"    157
"Cyprus"    102
"Romania"   255

想要的结果:

"Monaco"    35
"Montenegro"    66

1 个答案:

答案 0 :(得分:1)

假设您要查找区域内点总数超过区域外总数的情况,则此查询可能对您有用:

MATCH (c:Country)-[:vote_from]->(vP:vote_points)-[:vote_for]->(c2:Country)
WITH c2,
  REDUCE(s={in: 0, out: 0}, x IN COLLECT({reg: c.region, pts: toInteger(vP.points)}) |
    CASE WHEN x.reg=c2.region
      THEN {in: s.in + x.pts, out: s.out}
      ELSE {in: s.in, out: s.out + x.pts}
    END) AS res
WHERE res.in > res.out
RETURN c2.name AS name, res.in AS points