题干
代码思路
select distinct l1.Num as ConsecutiveNums
from Logs l1,Logs l2,Logs l3
where l1.Id+1=l2.id and l2.id+1=l3.id
and l1.Num =l2.Num and l2.Num=l3.Num
select
distinct num as 'ConsecutiveNums'
from (
select
num, id - cast(rank() over(partition by num order by id) as signed) as 'g'
from Logs
) as t
group by num, g
having count(1) >= 3
with t1 as
( select
num,
id - cast(dense_rank() over (order by num,id) as signed Integer) tmp
from logs
)
select
distinct num ConsecutiveNums
from t1
group by tmp, num
having count(1) > 2;
使用 lead 开窗 的核心为 对 num 进行分组 并 对num进行分组并对 组内的 id 进行排序, 去排序后移两位的id, 如果其等于2, 则表示连续.sql如下:
with tmp as (
select num,
lead(id, 2) over (partition by num order by id ) - id sub
from logs
)
select distinct num ConsecutiveNums from tmp where sub = 2 ;
emmmm开窗还是不懂啊
题干
代码思路
DENSE_RANK()
:select score ,dense_rank() over(order by score desc) as rankfrom Scores order by score desc
select a.Score as Score,
(select count(distinct b.Score) from Scores b where b.Score >= a.Score) as Rank
from Scores a
order by a.Score DESC