SQL技巧笔记(一):连续3人的连号问题—— LeetCode601.体育馆的人流量

SQL 技巧笔记

前言:我发现大数据招聘岗位上的应聘流程都是需要先进行笔试,其中占比很大的部分是SQL题目,经过一段时间的学习之后,今天开了一个力扣年会员,我觉得我很有必要去多练习笔试题目,这些题目是有技巧性的,很贴近生活!

Tips:我很享受独自做出题目的感觉,也很喜欢和大家分享自己的思路!我会继续努力,遇到有趣的题目,独特的思路会和大家多多交流!

一、连续 3 人的连号问题

1. 题目来源

  • LeetCode 601.体育馆的人流量
  • 困难型题目
  • 网易公司的笔试题

2. 题目描述

表:Stadium

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| visit_date    | date    |
| people        | int     |
+---------------+---------+
visit_date 是该表中具有唯一值的列。
每日人流量信息被记录在这三列信息中:序号 (id)、日期 (visit_date)、 人流量 (people)
每天只有一行记录,日期随着 id 的增加而增加

编写解决方案找出每行的人数大于或等于 100id 连续的三行或更多行记录。

返回按 visit_date 升序排列 的结果表。

查询结果格式如下所示。

示例 1:

输入:
Stadium 表:
+------+------------+-----------+
| 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       |
+------+------------+-----------+
输出:
+------+------------+-----------+
| id   | visit_date | people    |
+------+------------+-----------+
| 5    | 2017-01-05 | 145       |
| 6    | 2017-01-06 | 1455      |
| 7    | 2017-01-07 | 199       |
| 8    | 2017-01-09 | 188       |
+------+------------+-----------+
解释:
id 为 5、6、7、8 的四行 id 连续,并且每行都有 >= 100 的人数记录。
请注意,即使第 7 行和第 8 行的 visit_date 不是连续的,输出也应当包含第 8 行,因为我们只需要考虑 id 连续的记录。
不输出 id 为 2 和 3 的行,因为至少需要三条 id 连续的记录。

数据源:

Create table If Not Exists Stadium (id int, visit_date DATE NULL, people int);
Truncate table Stadium;
insert into Stadium (id, visit_date, people) values ('1', '2017-01-01', '10');
insert into Stadium (id, visit_date, people) values ('2', '2017-01-02', '109');
insert into Stadium (id, visit_date, people) values ('3', '2017-01-03', '150');
insert into Stadium (id, visit_date, people) values ('4', '2017-01-04', '99');
insert into Stadium (id, visit_date, people) values ('5', '2017-01-05', '145');
insert into Stadium (id, visit_date, people) values ('6', '2017-01-06', '1455');
insert into Stadium (id, visit_date, people) values ('7', '2017-01-07', '199');
insert into Stadium (id, visit_date, people) values ('8', '2017-01-09', '188');

3. 题目理解

  • 需求一:编写解决方案找出每行的人数大于或等于 100

  • 需求二:且 id 连续的三行或更多行记录。

  • 需求三:返回按 visit_date 升序排列 的结果表。


4. 思路顺序

(1) 筛选每行的人数大于或等于 100

代码:

SELECT
    *
FROM Stadium WHERE people >=100

效果:发现 id 为 5, 6, 7, 8 满足至少连 3 号

在这里插入图片描述


(2) 找出 id,前一个 id,后一个 id

代码:

SELECT
           id,
           LAG(id,1) OVER(ORDER BY id) as pre_id, # 前一个id
           LEAD(id,1) OVER(ORDER BY id) as next_id, # 后一个id
           visit_date,
           people
           FROM Stadium WHERE people >=100

效果:发现最前面的id 的前一个id为null,最后一个id的后一个id为null

在这里插入图片描述


(3) 找出三个id之间的关系

代码:

with t1 as(
    SELECT
           id,
           LAG(id,1) OVER(ORDER BY id) as pre_id, # 前一个id
           LEAD(id,1) OVER(ORDER BY id) as next_id, # 后一个id
           visit_date,
           people
           FROM Stadium WHERE people >=100
)
select id,visit_date,people from t1
where
(id = pre_id + 1 and id = next_id - 1) OR # 当前 id 是连续序列的中间部分
(next_id = id + 1 and pre_id is null ) OR # 当前 id 是连续序列的最开始部分
(pre_id = id - 1 and next_id is null)    # 当前 id 是连续序列的最结束部分
order by id;

效果:发现原本需要的 5 居然不见了,明显找三者关系条件远远不够

在这里插入图片描述


(4) 找出五个id之间的关系

代码:

with t1 as(
    SELECT
           id,
           LAG(id,1) OVER(ORDER BY id) as pre_id, # 前一个id
           LAG(id,2) OVER (ORDER BY id) as pre_2_id, # 前两个id
           LEAD(id,1) OVER(ORDER BY id) as next_id, # 后一个id
           LEAD(id,2) OVER(ORDER BY id) as next_2_id, # 后两个id
           visit_date,
           people
           FROM Stadium WHERE people >=100
)
select id,visit_date,people from t1
where
(id = pre_id + 1 and id = next_id - 1) OR # 当前 id 是连续序列的中间部分
(pre_id is null and next_id = id + 1 and next_2_id = id + 2 ) OR # 当前 id 是连续序列的最开始部分
(next_id = id + 1 and next_2_id = id + 2 ) OR # 当前 id 是连续序列的最开始部分
(id = pre_id + 1 and next_id is NULL and pre_2_id = id - 2) OR  # 当前 id 是连续序列的最结束部分
(pre_id = id - 1 and pre_2_id = id - 2)    # 当前 id 是连续序列的最结束部分
order by id;

效果:答案正确,3 个id的联系需要考虑极端情况,所以一共需要 5个 条件!


5. 提交答案

效果展示:经过20分钟思考,解题结果提交通过!

对比官方:官方的答案很简略,不过我觉得自己想出来的思路很有趣哦!

# 官方答案

select distinct t1.*
from stadium t1, stadium t2, stadium t3
where t1.people >= 100 and t2.people >= 100 and t3.people >= 100
and
(
	(t1.id - t2.id = 1 and t1.id - t3.id = 2 and t2.id - t3.id =1)  -- t1, t2, t3
    or
    (t2.id - t1.id = 1 and t2.id - t3.id = 2 and t1.id - t3.id =1) -- t2, t1, t3
    or
    (t3.id - t2.id = 1 and t2.id - t1.id =1 and t3.id - t1.id = 2) -- t3, t2, t1
)
order by t1.id;

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 01:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 01:40:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 01:40:01       82 阅读
  4. Python语言-面向对象

    2024-03-10 01:40:01       91 阅读

热门阅读

  1. python脚本批量关闭exe文件

    2024-03-10 01:40:01       39 阅读
  2. 用 reduce 实现 map 的功能

    2024-03-10 01:40:01       48 阅读
  3. 【C#语言入门】13. 表达式、语句详解(3)

    2024-03-10 01:40:01       50 阅读
  4. 基于单片机的输液监测系统设计与实现

    2024-03-10 01:40:01       41 阅读
  5. 鸿蒙崛起:能否颠覆安卓霸主地位?

    2024-03-10 01:40:01       47 阅读
  6. mongodb的备份与恢复

    2024-03-10 01:40:01       43 阅读
  7. python中的模块和包

    2024-03-10 01:40:01       50 阅读