Skip to content

Commit 0b08ae0

Browse files
authored
Update Human traffic of stadium.sql
1 parent ea2a1c8 commit 0b08ae0

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Hard/Human traffic of stadium.sql

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@
3030
-- Each day only have one row record, and the dates are increasing with id increasing.
3131

3232
-- Solution
33-
select a.id, a.visit_date, a.people
34-
from
35-
(select id, visit_date, people, id - row_number() over(order by visit_date) as dates
36-
from stadium
37-
where people>=100) a
38-
left join
39-
(select b.dates, count(*) as total
40-
from
41-
(select id, visit_date, people, id - row_number() over(order by visit_date) as dates
42-
from stadium
43-
where people>=100) b
44-
group by dates) c
45-
on a.dates = c.dates
46-
where c.total>2
33+
WITH t1 AS (
34+
SELECT id,
35+
visit_date,
36+
people,
37+
id - ROW_NUMBER() OVER(ORDER BY visit_date) AS dates
38+
FROM stadium
39+
WHERE people >= 100)
40+
41+
SELECT t1.id,
42+
t1.visit_date,
43+
t1people
44+
FROM t1
45+
LEFT JOIN (
46+
SELECT dates,
47+
COUNT(*) as total
48+
FROM t1
49+
GROUP BY dates) AS b
50+
USING (dates)
51+
WHERE b.total > 2

0 commit comments

Comments
 (0)