LeetCode MySQL-601. Human Traffic of Stadium

    LeetCode MySQL

    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.

    寫一個 SQL 查詢顯示三行或三行以上的 id 連續記錄,且每人的人數大於100人。
    依照 visit_date 進行升冪排序結果。
    

    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.
    

    Example 1:

    Input: 
    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       |
    +------+------------+-----------+
    Output: 
    +------+------------+-----------+
    | id   | visit_date | people    |
    +------+------------+-----------+
    | 5    | 2017-01-05 | 145       |
    | 6    | 2017-01-06 | 1455      |
    | 7    | 2017-01-07 | 199       |
    | 8    | 2017-01-09 | 188       |
    +------+------------+-----------+
    Explanation: 
    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.
    

    Solution:
    1. 選擇標題分別為 t1.id, t1.visit_date, t1.people。
    2. 來自 Stadium 的 table,三個相同的表互相比較。
    3. 確保三個表中的人數皆大於100。
    4. 確保連續三個數值,且將條件以[*, @, #]來代表成立範圍。

    Stadium table:
    +------+------------+-----------+---------+---------+---------+
    | id   | visit_date | people    | t1      | t2      | t3      |
    +------+------------+-----------+---------+---------+---------+
    | 1    | 2017-01-01 | 10        | NULL    | NULL    | NULL    |
    | 2    | 2017-01-02 | 109       | 109     | 109     | 109     |
    | 3    | 2017-01-03 | 150       | 150     | 150     | 150     |
    | 4    | 2017-01-04 | 99        | NULL    | NULL    | NULL    |
    | 5*   | 2017-01-05 | 145       | 145*@   | 145@    | 145     |
    | 6*@  | 2017-01-06 | 1455      | 1445*@# | 1445*   | 1445#   |
    | 7@#  | 2017-01-07 | 199       | 199*@#  | 199*#   | 199*@   |
    | 8#   | 2017-01-09 | 188       | 188*#   | 188     | 188*    |
    +------+------------+-----------+---------+---------+---------+
    

    Code.1:

    SELECT DISTINCT t1.id, t1.visit_date, t1.people
    	FROM Stadium AS t1, Stadium AS t2, Stadium AS t3
    WHERE (t1.people >= 100 AND t2.people >= 100 AND t3.people >= 100)
    	AND ((t1.id + 1 = t2.id AND t1.id + 2 = t3.id) OR
    	     (t1.id - 1 = t2.id AND t1.id + 1 = t3.id) OR
                 (t1.id - 1 = t2.id AND t1.id - 2 = t3.id))
    ORDER BY id;