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

Multiple UNWIND statements causing wrong output in Neo4j

我是 运行 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 语句单独运行良好,但是当我将它们放在一起时,就会出现重复问题。
谁能告诉我为什么会这样,我该如何解决?

感谢您提供数据和加载语句。我更容易处理数据并提出解决方案。基本上,我收集了主队和进球,然后将其与客队结合起来进行同一场比赛并获得进球。我通过 id 下订单,以便我可以手动检查结果的正确性,但如果您愿意,可以将其删除。

备注: 重复的来源来自我们获得 tgoals 的行。每个主队和客队都有两个值,因为我们在相同的路径 (x) 上计算它。所以我们得到像 Home, 1 , -1 。 Away -1, 1。当你单独展开它时,它会将 1 和 -1 分配给 Home,将 1 和 -1 分配给 Away。所以为了解决这个重复问题,我们创建了一对(称为列表)Home、tgoal 和 Away tgoal。这样,我们只有一个主场目标和一个客场目标。我将这本字典 (tname, tgoal) 称为组合。 "collected" 通过获取所有主队和他们的目标,然后添加客队和他们的目标。一旦将它们放在一个集合中,将第一个元素 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       │
├───────────────────────────┼────────┤