mysql查询连续三天100以上_输出连续大于等于三天销售量大于100的日期

CREATE TABLE t_log(f_time DATE, f_value int)

insert into T_LOG (F_TIME, F_VALUE)values ('2016-10-01', 100);

insert into T_LOG (F_TIME, F_VALUE)values ('2016-10-02', 101);

insert into T_LOG (F_TIME, F_VALUE)values ('2016-10-03', 102);

insert into T_LOG (F_TIME, F_VALUE)values ('2016-10-04', 99);

insert into T_LOG (F_TIME, F_VALUE)values ('2016-10-05', 105);

insert into T_LOG (F_TIME, F_VALUE)values ('2016-10-06', 108);

insert into T_LOG (F_TIME, F_VALUE)values ('2016-10-07', 105);

insert into T_LOG (F_TIME, F_VALUE)values ('2016-10-08', 199);

insert into T_LOG (F_TIME, F_VALUE)values ('2016-10-09', 108);

with a as(

select f_group,lead(f_group,1,'9999-01-01') over (order by f_group) as f_grouptwo from

(select F_TIME, F_VALUE,case when lag(f_value,1,0)over (order by f_time)<100 then f_time else null end f_group from t_log)t

where f_group is not null)

, b as(

select * from a

where datediff(dd,f_group,f_grouptwo)>=3)

select f_group,max(f_time) as maxtime

from b

inner join t_log c

on b.f_group<=c.f_time and b.f_grouptwo>c.f_time

where c.f_value>=100

group by f_group

现在的输出:

f_group maxtime

2016-10-01 2016-10-03

2016-10-05 2016-10-09

目标输出

2016-10-01

2016-10-02

2016-10-03

2016-10-05

2016-10-06

2016-10-07

2016-10-08

2016-10-09

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值