프로그래밍연습/SQL
[SQL]consecutive-numbers
Q_jihe
2023. 8. 17. 14:49
https://leetcode.com/problems/consecutive-numbers
Consecutive Numbers - LeetCode
Can you solve this real interview question? Consecutive Numbers - Table: Logs +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | num | varchar | +-------------+---------+ In SQL, id is the primary key for this table.
leetcode.com
with base as (
select
num
, lag(num, 1) over (order by id) rn1
, lag(num, 2) over (order by id) rn2
from logs
)
select
distinct num as 'ConsecutiveNums'
from base
where num = rn1 and rn1 = rn2