我目前正在处理一个足球比赛数据集,并试图让Cypher以最多的连胜回报球队。
目前我有一个收集声明,其中会创建一个列表[0,1,1,0,1,1,1]
,其中'0'
表示丢失,'1'
表示胜利。我试图以最连续的胜利回归球队。
以下是我的代码目前的样子:
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]
)
非常感谢任何有关计算连续胜利的指导或帮助。
由于
答案 0 :(得分:1)
我回答了类似的问题here。
关键是使用来自APOC程序的apoc.coll.split()
,在0
上进行拆分,这将产生每条连胜一行(连续1' s)为value
。每个列表的大小是该条纹的连续获胜次数,因此只需获得最大大小:
// 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
答案 1 :(得分:0)
您的用例实际上并不需要检测最连续的1(并且也不需要使用REDUCE
)。
以下查询使用consW
直接计算每个团队的最大连续获胜次数(maxW
跟踪当前连续获胜次数,MATCH (team:TEAM)-[p:PLAYS]-(:TEAM)
WITH team, p
ORDER BY p.time ASC
WITH team,
REDUCE(s = {consW: 0, maxW: 0}, m IN COLLECT(p) |
CASE WHEN (team = startnode(m) AND (m.score1 > m.score2)) OR (team = endnode(m) AND (m.score2 > m.score1))
THEN {consW: s.consW+1, maxW: CASE WHEN s.consW+1 > s.maxW THEN s.consW+1 ELSE s.maxW END}
ELSE s
END).maxW AS most_consecutive_wins
RETURN team.name AS teamName, most_consecutive_wins;
是最大数量到目前为止发现的连续胜利):
{{1}}