做网站一定要虚拟主机吗,wordpress如何配置伪静态页面,dede模板 展柜网站源码,平台搭建与拆除SQL 技巧笔记 前言#xff1a;我发现大数据招聘岗位上的应聘流程都是需要先进行笔试#xff0c;其中占比很大的部分是SQL题目#xff0c;经过一段时间的学习之后#xff0c;今天开了一个力扣年会员#xff0c;我觉得我很有必要去多练习笔试题目#xff0c;这些题目是有技…SQL 技巧笔记 前言我发现大数据招聘岗位上的应聘流程都是需要先进行笔试其中占比很大的部分是SQL题目经过一段时间的学习之后今天开了一个力扣年会员我觉得我很有必要去多练习笔试题目这些题目是有技巧性的很贴近生活 Tips我很享受独自做出题目的感觉也很喜欢和大家分享自己的思路我会继续努力遇到有趣的题目独特的思路会和大家多多交流 文章目录 SQL 技巧笔记一、连续 3 人的连号问题1. 题目来源2. 题目描述3. 题目理解4. 思路顺序(1) 筛选每行的人数大于或等于 100(2) 找出 id前一个 id后一个 id(3) 找出三个id之间的关系(4) 找出五个id之间的关系 5. 提交答案 一、连续 3 人的连号问题
1. 题目来源
LeetCode 601.体育馆的人流量困难型题目网易公司的笔试题 2. 题目描述
表Stadium
------------------------
| Column Name | Type |
------------------------
| id | int |
| visit_date | date |
| people | int |
------------------------
visit_date 是该表中具有唯一值的列。
每日人流量信息被记录在这三列信息中序号 (id)、日期 (visit_date)、 人流量 (people)
每天只有一行记录日期随着 id 的增加而增加编写解决方案找出每行的人数大于或等于 100 且 id 连续的三行或更多行记录。
返回按 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
代码
SELECTid,LAG(id,1) OVER(ORDER BY id) as pre_id, # 前一个idLEAD(id,1) OVER(ORDER BY id) as next_id, # 后一个idvisit_date,peopleFROM Stadium WHERE people 100效果发现最前面的id 的前一个id为null最后一个id的后一个id为null (3) 找出三个id之间的关系
代码
with t1 as(SELECTid,LAG(id,1) OVER(ORDER BY id) as pre_id, # 前一个idLEAD(id,1) OVER(ORDER BY id) as next_id, # 后一个idvisit_date,peopleFROM 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(SELECTid,LAG(id,1) OVER(ORDER BY id) as pre_id, # 前一个idLAG(id,2) OVER (ORDER BY id) as pre_2_id, # 前两个idLEAD(id,1) OVER(ORDER BY id) as next_id, # 后一个idLEAD(id,2) OVER(ORDER BY id) as next_2_id, # 后两个idvisit_date,peopleFROM 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, t3or(t2.id - t1.id 1 and t2.id - t3.id 2 and t1.id - t3.id 1) -- t2, t1, t3or(t3.id - t2.id 1 and t2.id - t1.id 1 and t3.id - t1.id 2) -- t3, t2, t1
)
order by t1.id;