Human Traffic of Stadium : LeetCode

Shefali Bisht
Geek Culture
Published in
3 min readMay 11, 2022

Difficulty : Hard

Photo by Caspar Camille Rubin on Unsplash

Problem Statement: Write an SQL query to display the records with three or more rows with consecutive id's, and the number of people is greater than or equal to 100 for each.

Return the result table ordered by visit_date in ascending order.

Input :

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| visit_date | date |
| people | int |
+---------------+---------+
visit_date is the primary key for this table.
Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit.
No two rows will have the same visit_date, and as the id increases, the dates increase as well.

Example :

Input: 
Stadium table:
+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 1 | 2017-01-01 | 10 |
| 2 | 2017-01-02 | 109 |
| 3 | 2017-01-03 | 150 |
| 4 | 2017-01-04 | 99 |
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+-----------+
Output:
+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+-----------+
Explanation:
The four rows with ids 5, 6, 7, and 8 have consecutive ids and each of them has >= 100 people attended. Note that row 8 was included even though the visit_date was not the next day after row 7.
The rows with ids 2 and 3 are not included because we need at least three consecutive ids.

Solution :-

Step 1 : We will be using WINDOW() functions to get the preceding and forward values.

We require both LAG() and LEAD() and used filter condition so that we can fetch consecutive IDs where people count is greater than 100.

with cte as (
select id,
visit_date,
people,
LAG(id,1) OVER(order by id) as prevID_1,
LAG(id,2) OVER(order by id) as prevID_2,
LEAD(id,1) OVER(order by id) as nextID_1,
LEAD(id,2) OVER(order by id) as nextID_2
from Stadium
where people>=100
)

Step 2 : Use CASE conditions to separate first, middle and last row in a 3 consecutive series.

WHEN id+1=nextID_1 AND id+2 = nextID_2 -> first row
WHEN id-1=prevID_1 AND id-2 = prevID_2 -> last row
WHEN id-1 = prevID_1 and id+1=nextID_1 -> middle row

,cte2 as (
select *,
CASE WHEN id+1=nextID_1 AND id+2 = nextID_2 then 'Y'
WHEN id-1=prevID_1 AND id-2 = prevID_2 then 'Y'
WHEN id-1 = prevID_1 and id+1=nextID_1 then 'Y'
ELSE 'N' END as flag
from cte
)

Step 3 : Now combine both CTEs

with filtered_data as (
select id,
visit_date,
people,
LAG(id,1) OVER(order by id) as prevID_1,
LAG(id,2) OVER(order by id) as prevID_2,
LEAD(id,1) OVER(order by id) as nextID_1,
LEAD(id,2) OVER(order by id) as nextID_2
from Stadium
where people>=100
), ordered_filtered_data as (
select *,
CASE WHEN id+1=nextID_1 AND id+2 = nextID_2 then 'Y'
WHEN id-1=prevID_1 AND id-2 = prevID_2 then 'Y'
WHEN id-1 = prevID_1 and id+1=nextID_1 then 'Y'
ELSE 'N' END as flag
from filtered_data
)
select id, visit_date, people from ordered_filtered_data where flag = 'Y'

Check this link for the complete code..

LeetCode Problem Output :

If you found this article helpful, share it with your friends and colleagues. If you have any other questions, you can find me on Linkedin .

--

--

Shefali Bisht
Geek Culture

Data Engineer who loves experimenting with different datasets and technologies to make your life easy and mine complex. https://www.shefalibisht.com/