Skip to content

Commit 8d60ed9

Browse files
committed
task: #601
1 parent 5924813 commit 8d60ed9

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
180180
- [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql)
181181
3. [Hard](./leetcode/hard/)
182182
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
183+
- [601. Human Traffic of Stadium](./leetcode/hard/601.%20Human%20Traffic%20of%20Stadium.sql)
183184
- [3374. First Letter Capitalization II](./leetcode/hard/3374.%20First%20Letter%20Capitalization%20II.sql)
184185

185186
## Contributing
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Question 601. Human Traffic of Stadium
3+
Link:
4+
5+
Table: Stadium
6+
7+
+---------------+---------+
8+
| Column Name | Type |
9+
+---------------+---------+
10+
| id | int |
11+
| visit_date | date |
12+
| people | int |
13+
+---------------+---------+
14+
visit_date is the column with unique values for this table.
15+
Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit.
16+
As the id increases, the date increases as well.
17+
18+
19+
Write a solution 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.
20+
21+
Return the result table ordered by visit_date in ascending order.
22+
*/
23+
24+
WITH more_than_100 AS (
25+
SELECT
26+
s1.id,
27+
s1.id - ROW_NUMBER() OVER (ORDER BY s1.id) AS c_id
28+
FROM Stadium AS s1
29+
WHERE s1.people >= 100
30+
),
31+
32+
three_consecutive AS (
33+
SELECT s2.c_id
34+
FROM more_than_100 AS s2
35+
GROUP BY s2.c_id
36+
HAVING COUNT(s2.c_id) >= 3
37+
)
38+
39+
SELECT
40+
s.id,
41+
s.visit_date,
42+
s.people
43+
FROM Stadium AS s
44+
INNER JOIN
45+
more_than_100 AS mt
46+
ON s.id = mt.id
47+
WHERE mt.c_id IN (
48+
SELECT tc.c_id
49+
FROM three_consecutive AS tc
50+
)

0 commit comments

Comments
 (0)