[英]List the most consecutive wins in cypher
我正在努力为团队争取最连续的胜利。 首先,我指定了每个团队的胜利和失败结果,但是我不知道如何获得团队中最连续的胜利
MATCH (a:TeamFootbal )-[r]->(m:Games)<-[r2]-(op:TeamFootbal)
with a.name as teamnames,
case when r.scores > r2.scores then 1 else 0 end as result
return teamnames, result
order by teamnames
输出将是这样的
Team Name Result
A 1
A 1
A 1
A 0
A 1
B 1
B 1
B 1
B 1
B 0
C 1
C 0
C 1
C 1
C 0
D 0
D 1
D 0
D 0
D 1
E 1
E 1
E 1
E 1
E 0
我想得到
B 4
E 4
无需使用apoc程序
我将答案改编成类似的问题:
MATCH (a:TeamFootbal)-[r]->(m:Games)<-[r2]-(op:TeamFootbal)
WITH
a.name AS teamname,
CASE WHEN r.scores > r2.scores THEN 1 ELSE 0 END AS result
ORDER BY m.date ASC // (*)
WITH teamname AS s, collect([teamname, result]) AS p
WITH s, reduce(acc = [], i IN range(0, size(p) - 1) |
CASE p[i] = p[i-1]
WHEN true THEN [j IN range(0, size(acc) - 1) |
CASE j = size(acc) - 1
WHEN true THEN acc[j] + [p[i]]
ELSE acc[j]
END
]
ELSE acc + [[p[i]]]
END
) AS streaks
UNWIND streaks AS streak
WITH s, streak
WHERE streak[0] <> 0
RETURN s, max(size(streak)) AS consecutivePasses
请注意,订购应插入(*)
这是在列表中搜索以下1
的最大数量的小技巧:
WITH [1,0,0,1,1,1,0,1,1,1,1] AS results
RETURN
reduce(
// current value, highest value so far
x=[0,0],
i IN results |
CASE
WHEN i=0 THEN [0 , x[1]]
WHEN (x[0]+i) > x[1] THEN [x[0] +1 , x[0]+1]
ELSE [x[0] +1 , x[1]]
END
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.