返回介绍

solution / 0600-0699 / 0601.Human Traffic of Stadium / README_EN

发布于 2024-06-17 01:03:36 字数 2696 浏览 0 评论 0 收藏 0

601. Human Traffic of Stadium

中文文档

Description

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.

The result format is in the following example.

 

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.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  S AS (
    SELECT
      *,
      id - (ROW_NUMBER() OVER (ORDER BY id)) AS rk
    FROM Stadium
    WHERE people >= 100
  ),
  T AS (SELECT *, COUNT(1) OVER (PARTITION BY rk) AS cnt FROM S)
SELECT id, visit_date, people
FROM T
WHERE cnt >= 3
ORDER BY 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文