【笔记】Oracle Offset
offset 代表跳过前 n 行,如果表少于 n+1 条记录,结果集将是空的;比如 n = 100,表示从 101 开始往后查。
fetch next 代表往后查 n 行;
next 和 first 可互换;
row 和 rows 可互换;
offset 语法:
类似于
-- 从第20条开始取下10条记录
SELECT *
FROM table_name
OFFSET 20 ROWS
FETCH NEXT 10 ROWS ONLY;
力扣题目:https://leetcode.cn/problems/human-traffic-of-stadium/
编写一个 SQL 查询以找出每行的人数大于或等于 100 且 id 连续的三行或更多行记录。
返回按 visit_date 升序排列 的结果表
/*简单来说就是把当前行的前两行和后两行都用开窗函数拿到,然后查一下有没有三行都是大于100的,就有了,因为就算是五行连续的,那也会因为每一行的数据而取出来*/
select id, visit_date, p3 as people from (
select
id, to_char(visit_date, 'yyyy-MM-dd') as visit_date,
lag(people, 2, null) over(order by id) as p1,
lag(people, 1, null) over(order by id) as p2,
people as p3,
lead(people, 1, null) over(order by id) as p4,
lead(people, 2, null) over(order by id) as p5
from stadium
) where (p1 >= 100 and p2 >= 100 and p3 >= 100)
or (p2 >= 100 and p3 >= 100 and p4 >= 100)
or (p3 >= 100 and p4 >= 100 and p5 >= 100)
标签:p3,people,over,visit,力扣,Offset,Oracle,100,id
From: https://www.cnblogs.com/jokingremarks/p/16744729.html