我在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的行。每个Home和Away团队都有两个值,因为我们在同一路径(x)上计算它。所以我们得到Home,1,-1。离开-1,1。当你单独展开它时,它将1和-1分配给Home,1和-1分配给Away。因此,要修复此重复,我们创建了一对(称为列表)Home,tgoal和Away tgoal。通过这种方式,我们只获得一个家庭用户和一个用于离开的用户。我把这个词典(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 │
├───────────────────────────┼────────┤