可以组合这两个查询

时间:2015-01-07 21:18:24

标签: neo4j cypher

我有两个密码查询,想知道是否有可能在一个语句中获取Points和HeadHunter信息。

MATCH (s:SEASON)-[*]->(e:EVENT)<-[f:FINISHED]-(p:PLAYER)
    WHERE s.id = 8 AND e.id <= 1197
    RETURN p, sum(f.points) AS Points
    ORDER BY Points DESC

MATCH (s:SEASON)-[*]->(e:EVENT)<-[el:ELIMINATED]-(p:PLAYER)
    WHERE s.id = 8 AND e.id <= 1197
    RETURN p, sum(el.points) AS HeadHunter
    ORDER BY HeadHunter DESC

我玩了不同的方法,但没有一个工作。我想做这样的事情:

MATCH (s:SEASON)-[*]->(e:EVENT)<-[el:ELIMINATED]-(p:PLAYER),(e)<-[f:FINISHED]-(p)
    WHERE s.id = 8 AND e.id <= 1197
    RETURN p, sum(el.points) AS HeadHunter, sum(f.points) AS Points

2 个答案:

答案 0 :(得分:3)

这对你有用吗?

MATCH (s:SEASON)-[*]->(e:EVENT)<-[rel:FINISHED|ELIMINATED]-(p:PLAYER)
WHERE s.id = 8 AND e.id <= 1197
WITH p, COLLECT(rel) AS rels
RETURN p,
  REDUCE(s = 0, x IN rels | CASE WHEN TYPE(x) = 'FINISHED' THEN s + x.points ELSE s END) AS Points,
  REDUCE(s = 0, x IN rels | CASE WHEN TYPE(x) = 'ELIMINATED' THEN s + x.points ELSE s END) AS HeadHunter

这应该返回已完成和/或被淘汰的每个玩家的相关金额。

答案 1 :(得分:1)

原则上我认为您的查询可以正常运行,但您的p变量可能会出错。在此查询中:

MATCH (s:SEASON)-[*]->(e:EVENT)<-[el:ELIMINATED]-(p:PLAYER),(e)<-[f:FINISHED]-(p)
    WHERE s.id = 8 AND e.id <= 1197
    RETURN p, sum(el.points) AS HeadHunter, sum(f.points) AS Points

请注意,p必须是ELIMINATED关系 FINISHED关系的玩家。我根据您域名的语义进行猜测,这是没有意义的;你不能被淘汰并完成,对吗?

所以你可能想尝试一些变体:

MATCH (s:SEASON)-[*]->(e:EVENT)<-[el:ELIMINATED]-(pElim:PLAYER),(e)<-[f:FINISHED]-(pFinish)
    WHERE s.id = 8 AND e.id <= 1197
    RETURN pElim, sum(el.points) AS HeadHunter, pFinish, sum(f.points) AS Points

请注意,对于pElimpFinish的各种组合,您会有很多额外的输出行,但相应的值应该是正确的。