diff --git a/README.md b/README.md index 4179849..1ef55db 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,7 @@ Useful for preparing for technical interviews and improving your SQL skills. - [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql) 3. [Hard](./leetcode/hard/) - [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql) + - [601. Human Traffic of Stadium](./leetcode/hard/601.%20Human%20Traffic%20of%20Stadium.sql) - [3374. First Letter Capitalization II](./leetcode/hard/3374.%20First%20Letter%20Capitalization%20II.sql) ## Contributing diff --git a/leetcode/hard/601. Human Traffic of Stadium.sql b/leetcode/hard/601. Human Traffic of Stadium.sql new file mode 100644 index 0000000..93ef9f4 --- /dev/null +++ b/leetcode/hard/601. Human Traffic of Stadium.sql @@ -0,0 +1,50 @@ +/* +Question 601. Human Traffic of Stadium +Link: + +Table: Stadium + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| id | int | +| visit_date | date | +| people | int | ++---------------+---------+ +visit_date is the column with unique values 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. +As the id increases, the date increases as well. + + +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. + +Return the result table ordered by visit_date in ascending order. +*/ + +WITH more_than_100 AS ( + SELECT + s1.id, + s1.id - ROW_NUMBER() OVER (ORDER BY s1.id) AS c_id + FROM Stadium AS s1 + WHERE s1.people >= 100 +), + +three_consecutive AS ( + SELECT s2.c_id + FROM more_than_100 AS s2 + GROUP BY s2.c_id + HAVING COUNT(s2.c_id) >= 3 +) + +SELECT + s.id, + s.visit_date, + s.people +FROM Stadium AS s +INNER JOIN + more_than_100 AS mt + ON s.id = mt.id +WHERE mt.c_id IN ( + SELECT tc.c_id + FROM three_consecutive AS tc +)