请帮助我获取条纹数据。我有目标成就表
表测试
dt
2017-01-01
2017-01-02
2017-01-03. //3 days end of streak
2017-02-10 // 1 day
2017-02-15
2017-02-16
2017-02-17
2017-02-18 //4 days
我在MySQL中尝试过
Select dt, (select count(*) from test as t1 where t1.dt < t2.dt and datediff(t2.dt,t1.dt) =1) as str 
from test as t2
并得到
Dt         str
2017-01-01  0
2017-01-02  1
2017-01-03  2
2017-02-10  0
2017-02-15  0
2017-02-16  1
2017-02-17  2
2017-02-18  3
是否有可能得到这样的东西
Dt.        Str 
2017-01-03  3
2017-02-10  1
2017-02-18  4
并获得最大收益?
您可以从当前行的日期中减去行号(即,行数<=当前行的日期),以将相差一天的连续行分类为同一组。然后,仅是计算计数的分组操作。
select max(dt) as dt, count(*) as streak
from (select t1.dt
      ,date(t1.dt,-(select count(*) from t t2 where t2.dt<=t1.dt)||' day') as grp
      from t t1
     ) t
group by grp 
运行内部查询以查看如何分配组。
| 归档时间: | 
 | 
| 查看次数: | 438 次 | 
| 最近记录: |