Neo4j连续列表 [英] Neo4j List Consecutive
问题描述
我目前正在处理足球比赛数据集,并试图让Cypher退回获得最多连续胜利的球队.
I am currently working with a football match data set and trying to get Cypher to return the teams with the most consecutive wins.
目前,我有一个收集语句,该语句创建一个列表,即[0,1,1,0,1,1,1]
,其中'0'
表示亏损,'1'
表示获胜.我想让球队获得连续最多的胜利.
At the moment I have a collect statement which creates a list i.e. [0,1,1,0,1,1,1]
where '0'
represents a loss and '1'
represents a win. I am trying to return the team with the most consecutive wins.
这是我目前的代码:
MATCH(t1:TEAM)-[p:PLAYS]->(t2:TEAM)
WITH [t1,t2] AS teams, p AS matches
ORDER BY matches.time ASC
UNWIND teams AS team
WITH team.name AS teamName, collect(case when ((team = startnode(matches)) AND (matches.score1 > matches.score2)) OR ((team = endnode(matches)) AND (matches.score2 > matches.score1)) then +1 else 0 end) AS consecutive_wins
RETURN teamName, consecutive_wins
这将返回每个团队的列表,以上述形式(即[0,1,0,1,1,0]
)显示他们的赢/失败记录
This returns a list for each team showing their win / lose record in the form explained above (i.e. [0,1,0,1,1,0]
)
在计算连续获胜方面的任何指导或帮助将不胜感激.
Any guidance or help in regards to calculating consecutive wins would be much appreciated.
谢谢
推荐答案
我在此处回答了类似的问题.
I answered a similar question here.
关键是使用APOC程序中的apoc.coll.split()
,在0
上分割,这将在每个连胜记录(连续1的列表)中产生一行作为value
.每个列表的大小是该连胜的连续获胜次数,因此只需获取最大大小即可:
The key is using apoc.coll.split()
from APOC Procedures, splitting on 0
, which will yield a row per winning streak (list of consecutive 1's) as value
. The size of each of the lists is the number of consecutive wins for that streak, so just get the max size:
// your query above
CALL apoc.coll.split(consecutive_wins, 0) YIELD value
WITH teamName, max(size(value)) as consecutiveWins
ORDER BY consecutiveWins DESC
LIMIT 1
RETURN teamName, consecutiveWins
这篇关于Neo4j连续列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!