I'm trying out this problem on LeetCode. The interface is throwing a syntax error, but for the life of me I can't figure it out. Here's the question:
Table: Stadium
+---------------+---------+
| 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.
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.
The query result format is in the following example.
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 |
+------+------------+-----------+
Result table:
+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+-----------+
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.
Here's a fiddle with the data (NOTE: I'm still trying to figure out how to insert dates, so I saved the dates as strings instead).
Can anyone spot the syntax error in the query below?
# Write your MySQL query statement below
SET @rowIndex := 0;
WITH s1 as (
SELECT @rowIndex := @rowIndex + 1 as rowIndex, s.*
FROM Stadium as s
WHERE s.people >= 100
GROUP BY s.id
)
SELECT s2.id, s2.visit_date, s2.people
FROM s1 as s2
GROUP BY s2.rowIndex - s2.id, s2.id, s2.visit_date, s2.people
ORDER BY s2.visit_date
Error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WITH s1 (rowIndex, id, visit_date, people) as ( SELECT @rowIndex := @rowInde' at line 4
Also, the LeetCode interface uses MySQL v8.0, so I don't think that's the problem.
I was using the query below as a reference. (Original.)
SET @rowIndex := -1;
SELECT ROUND(AVG(t.LAT_N), 4) FROM
(
SELECT @rowIndex := @rowIndex+1 AS rowIndex, s.LAT_N FROM STATION AS s ORDER BY s.LAT_N
) AS t
WHERE t.rowIndex IN (FLOOR(@rowIndex / 2), CEIL(@rowIndex / 2));
Thanks.
Edit:
For future reference, here's the final query I came up with:
# Write your MySQL query statement below
WITH s1 as (
SELECT ROW_NUMBER() OVER (ORDER BY s.id) as rowIndex, s.*
FROM Stadium as s
WHERE s.people >= 100
GROUP BY s.id, s.visit_date, s.people
), s2 as (
SELECT COUNT(s.id) OVER (PARTITION BY s.id-s.rowIndex) as groupSize, s.*
FROM s1 as s
)
SELECT s3.id, s3.visit_date, s3.people
FROM s2 as s3
GROUP BY s3.groupSize, s3.id, s3.visit_date, s3.people
HAVING s3.groupSize >= 3
ORDER BY s3.visit_date