KEEP GOING
[MySQL] LeetCode : Consecutive-Numbers 본문
반응형
https://leetcode.com/problems/consecutive-numbers/
1. 코드 구현 (CTE 사용)
WITH TMP AS(
SELECT id
,num
,LAG(num, 1, NULL) OVER(ORDER BY id)AS num2
,LAG(num, 2, NULL) OVER(ORDER BY id)AS num3
FROM Logs
)
SELECT DISTINCT num AS ConsecutiveNums
FROM TMP
WHERE num = num2 AND num2 = num3
2. 코드 구현 (FROM절 서브쿼리)
SELECT DISTINCT tmp.num AS ConsecutiveNums
FROM (
SELECT id
,num
,LAG(num, 1, NULL) OVER(ORDER BY id)AS num2
,LAG(num, 2, NULL) OVER(ORDER BY id)AS num3
FROM Logs
)tmp
WHERE tmp.num = tmp.num2 AND tmp.num2 = tmp.num3
반응형
'code review > sql' 카테고리의 다른 글
[MySQL] LeetCode : Nth highest Salary (0) | 2022.02.18 |
---|---|
[MySQL] 정규 표현식 정리(REGEXP) (0) | 2022.02.09 |
[MySQL] LeetCode : Department Top Three Salaries (0) | 2022.02.04 |
[MySQL] HackerRank : Challenges (0) | 2022.02.02 |
[python] LeetCode : zigzag-conversion (ceil(), join()) (0) | 2022.02.02 |
Comments