码迷,mamicode.com
首页 > 其他好文 > 详细

连续日期

时间:2021-05-24 01:17:27      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:class   blog   https   tps   方法   pre   span   with   rgba   

技术图片

 

 

 技术图片

 

 

 技术图片

 

 

 技术图片

方法一

select t1.*
from stadium t1, stadium t2, stadium t3
where t1.people >= 100 and t2.people >= 100 and t3.people >= 100
and
(
	  (t1.id - t2.id = 1 and t1.id - t3.id = 2 and t2.id - t3.id =1)  -- t1, t2, t3
    or
    (t2.id - t1.id = 1 and t2.id - t3.id = 2 and t1.id - t3.id =1) -- t2, t1, t3
    or
    (t3.id - t2.id = 1 and t2.id - t1.id =1 and t3.id - t1.id = 2) -- t3, t2, t1
)
;

方法二

with people as
(
    select id, visit_date, people,
    Lag(people,2) over(order by id) as pprvPeople,
    Lag(people,1) over(order by id) as prvPeople,
    Lead(people,1) over(order by id) as nextPeople,
    Lead(people,2) over(order by id) as nnextPeople
    from stadium
)
select id, visit_date, people from people
where 
(people >= 100 and prvPeople>=100 and pprvPeople>=100) ||
(people >= 100 and nextPeople>=100 and nnextPeople>=100) ||
(people >= 100 and nextPeople>=100 and prvPeople>=100) ;

方法三

with t as (
    select id,visit_date,people,cast(r as signed)-id df
    from
    (
    select id,visit_date,people,
    row_number() over (order by id) r
    from
    (
    select *
    from Stadium
    where people>=100
    ) a
    ) b
)
#这里r是unsigned
#这里可以进行优化
with t as (
    select id,visit_date,people,
    id-row_number() over (order by id) rk
    from Stadium
    where people>=100
)
select id,visit_date,people from t where df in ( select df from t group by df having count(*)>=3 );

连续日期

标签:class   blog   https   tps   方法   pre   span   with   rgba   

原文地址:https://www.cnblogs.com/hugrice/p/14743808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!