LeetCode.601.Human_Traffic_of_Stadium

601.Human_Traffic_of_Stadium

https://leetcode.com/problems/human-traffic-of-stadium/

Solution-for-3_or_more consecutive_visit_date

Create table temp_visit_date
 select
	b.id,
	b.visit_date,
	b.people,
	DATE_SUB(visit_date,INTERVAL b.rk DAY) as diffdate
from (
	select
	a.id, 
	a.visit_date, 
	a.people, 
	(select count(distinct visit_date) from stadium b where b.people >= 100 and b.visit_date <= a.visit_date) as rk
	from stadium a 
	where a.people >= 100
	order by visit_date ASC
) b;

select
id,
visit_date,
people
from (
	select diffdate, count(1) as cnt from temp_visit_date group by diffdate having cnt >= 3
)tt left join temp_visit_date aa on aa.diffdate = tt.diffdate
;

Solutionp-for-3_or_more consecutive_rows

# Write your MySQL query statement below
select
    zz.id,
    zz.visit_date,
    zz.people
from (
    select diff_row, count(1) as cnt from (
        select
        b.id,
        b.visit_date,
        b.people,
        (id-rk) as diff_row
    from (
        select
            a.id, 
            a.visit_date, 
            a.people, 
            (select count(distinct id) from stadium b where b.people >= 100 and b.id <= a.id) as rk
        from stadium a 
        where a.people >= 100
        order by id ASC
    ) b
    ) xx group by diff_row having cnt >= 3
) tt left join (
    select
        b.id,
        b.visit_date,
        b.people,
        (id-rk) as diff_row
    from (
        select
            a.id, 
            a.visit_date, 
            a.people, 
            (select count(distinct id) from stadium b where b.people >= 100 and b.id <= a.id) as rk
        from stadium a 
        where a.people >= 100
        order by id ASC
    ) b
) zz on zz.diff_row = tt.diff_row
;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值