列出密码中最连续的胜利

时间:2018-01-19 11:42:04

标签: database neo4j 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程序

2 个答案:

答案 0 :(得分:0)

我将answer改编为类似的问题:

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 :(得分:0)

这是一个搜索列表中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
  )