1

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
spheroidic
  • 199
  • 7
  • 1
    Please provide sample data and desired results. There is a better way to write this logic. – Gordon Linoff Oct 29 '20 at 19:28
  • 1
    The syntax error i receive when trying your code is "ERROR 1146 (42S02): Table 'test.stadium' doesn't exist" which means you did not provide full details with your question.... – Luuk Oct 29 '20 at 19:30
  • @GordonLinoff thanks for the comment. Happy to add the question details. I was hoping to spot the error in my query as written for learning purposes. Any idea what might be causing the error? – spheroidic Oct 29 '20 at 19:32
  • 1
    I suggest you run the query `SELECT @@version;` to confirm that the _server_ is 8.0. You might be using a MySQL 8.0 _client_ but that doesn't mean the `WITH` syntax will be supported. – Bill Karwin Oct 29 '20 at 20:19
  • @BillKarwin good point. Unfortunately it looks like the server is also 8.0. Result from the query: '{"headers": ["@@version"], "values": [["8.0.21"]]}' – spheroidic Oct 29 '20 at 20:22
  • 3
    Why not just use `row_number()` instead of the `@rowindex` hack? –  Oct 29 '20 at 21:43
  • 1
    @user8629729 MySQL 8.0 introduced common table expression alongside [ROW_NUMBER](https://stackoverflow.com/questions/1895110/row-number-in-mysql) which should simplify query – Lukasz Szozda Oct 29 '20 at 21:45
  • Thanks @a_horse_with_no_name, Lukasz Szozda, I will try ROW_NUMBER going forwards – spheroidic Oct 30 '20 at 16:12

2 Answers2

0
  • Not sure what problem you might be facing, yet this would pass on LeetCode:

  • Not sure if that's a right way to do it:

SELECT DISTINCT S1.id,
                S1.visit_date,
                S1.people
FROM stadium AS S1,
     stadium AS S2,
     stadium AS S3
WHERE S1.people > 99
  AND S2.people > 99
  AND S3.people > 99
  AND ( (S2.id = S1.id + 1
         AND S3.id = S1.id + 2)
       OR (S2.id = S1.id - 1
           AND S3.id = S1.id + 1)
       OR (S2.id = S1.id - 1
           AND S3.id = S1.id - 2) )
ORDER BY id ASC;
Emma
  • 27,428
  • 11
  • 44
  • 69
0

You confirmed that you are using MySQL 8.0.21 server so the only other suggestion I have is that you're trying to run two SQL statements in one call:

SET @rowIndex := 0;

WITH s1 as (
  SELECT...

Most MySQL connectors do not support multi-query by default. In other words, you can only do one statement per call. As soon as MySQL sees any syntax following your first ; it treats this as a syntax error.

There's no reason you need to use multi-query. Just run the two statements separately. As long as you use the same session, your value of @rowIndex will be available to subsequent statements.

The former Director of Engineering for MySQL once told me, "there's no reason multi-query should exist."

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Thanks adding an answer. It looks like removing the `;` after the SET assignment results in the same syntax error. ' I'm not familiar with multi-query but I'll bone up on it! – spheroidic Oct 29 '20 at 22:05
  • 1
    You misunderstand. It's not the semicolon that is the problem. It's everything following the semicolon. You can't do more than one statement per call. – Bill Karwin Oct 29 '20 at 22:26
  • I see... so if I want to stick with the user variable strategy, I could run the SET command, clear the editor, then run the second query. This is what you mean, correct? (I tried this to test and it worked! No syntax error.) – spheroidic Oct 30 '20 at 16:18