多个UNWIND语句导致Neo4j中的错误输出

时间:2019-04-23 13:17:15

标签: neo4j cypher graph-databases

我正在Neo4j上运行此密码查询

match x = (t1:Team)-[ho: Home]->(g: Game)<-[aw: Away]-(t2: Team)
with t1.name as tname1, 
     sum(toInteger(substring(g.full_time,0,1)) - 
     toInteger(substring(g.full_time,2,2))) as tgoals1, 
     t2.name as tname2, 
     sum(toInteger(substring(g.full_time,2,2))- 
     toInteger(substring(g.full_time,0,1))) as tgoals2
unwind [tgoals1 , tgoals2] as tgoals
unwind [tname1 , tname2] as tname
return tname

它给出这样的输出

"Arsenal FC"
"Leicester City FC"
"Arsenal FC"
"Leicester City FC"
"Brighton & Hove Albion FC"
"Manchester City FC"
"Brighton & Hove Albion FC"
"Manchester City FC"

实际上输出应该是这样的

"Arsenal FC"
"Leicester City FC"
"Brighton & Hove Albion FC"
"Manchester City FC"

如果我删除行

unwind [tgoals1 , tgoals2] as tgoals

输出变好了,但是我真正想要的是

return tname, tgoals

所以我无法删除它。 简而言之,两个UNWIND语句分别可以正常工作,但是当我将它们放在一起时,就会出现重复的问题。
谁能告诉我为什么会这样,怎么解决?

1 个答案:

答案 0 :(得分:0)

感谢您的数据和负载说明。对我来说,处理数据并提出解决方案比较容易。基本上,我收集了主队,然后将目标与客队组合在一起进行同一场比赛并获得了目标。我已按ID下订单,以便可以手动检查结果的正确性,但可以根据需要将其删除。

注意: 重复的来源来自获得目标的那一行。每个主队和客队都将具有两个值,因为我们在相同的路径(x)上对其进行计算。所以我们得到Home,1,-1。 Away -1,1.。分开展开时,它将为Home分配1和-1,为Away分配1和-1。因此,为解决此重复问题,我们创建了一个对(称为列表)Home,tgoal和Away tgoal。这样,我们只为Home获得1个目标,而Away获得1个目标。我称这本词典(tname,tgoal)为组合词典。通过收集所有主队及其目标,然后添加队及其目标来“收集”。将它们放入一个集合后,将第一个元素c [0]称为团队,将第二个元素称为目标。 Cypher就像python,其中列表的第一个元素的索引为0。

match (t1:Team)-[ho: Home]->(g: Game)
with t1.name as tname1, ID(g) as id1,
     toInteger(substring(g.full_time,0,1)) -
     toInteger(substring(g.full_time,2,2)) as tgoals1
match (t2:Team)-[aw: Away]->(g)
where ID(g) = id1
with tname1, tgoals1, t2.name as tname2, ID(g) as id2,
     collect ([tname1, tgoals1]) + collect([t2.name, toInteger(substring(g.full_time,2,2)) -
     toInteger(substring(g.full_time,0,1))]) as combo
unwind combo as c
return  c[0] as tname, c[1] as tgoals
order by id2

结果:(样本)

╒═══════════════════════════╤════════╕
│"tname"                    │"tgoals"│
╞═══════════════════════════╪════════╡
│"Arsenal FC"               │1       │
├───────────────────────────┼────────┤
│"Leicester City FC"        │-1      │
├───────────────────────────┼────────┤
│"Brighton & Hove Albion FC"│-2      │
├───────────────────────────┼────────┤
│"Manchester City FC"       │2       │
├───────────────────────────┼────────┤
│"Chelsea FC"               │-1      │
├───────────────────────────┼────────┤
│"Burnley FC"               │1       │
├───────────────────────────┼────────┤