728x90
# Write your MySQL query statement below
SELECT DISTINCT
l1.Num as ConsecutiveNums
FROM
Logs l1
JOIN Logs l2 ON l1.Id = l2.Id - 1
JOIN Logs l3 ON l2.Id = l3.Id - 1
WHERE
l1.Num = l2.Num AND l2.Num = l3.Num
;
self join 을 2번한다. 총 3개의 table이 join한다
결과
{"headers":
["Id", "Num", "Id", "Num", "Id", "Num"],
"values":
[[1, 1, 2, 1, 3, 1],
[2, 1, 3, 1, 4, 2],
[3, 1, 4, 2, 5, 1],
[4, 2, 5, 1, 6, 2],
[5, 1, 6, 2, 7, 2]]}
따라서 l1.num = l2.num = l2.num 인 row 중에서(WHERE)
l1.num을 distinct한 결과만 column rename(as) 해서 출력
728x90
'SQL' 카테고리의 다른 글
[LeetCode] Department Highest Salary ( sub query, partition by) (0) | 2021.04.10 |
---|---|
SQL 실행 순서와 문법 순서 (0) | 2021.04.10 |
[LeetCode] Rank Scores (rank, dense rank, row number) (0) | 2021.04.10 |
[LeetCode] Nth Highest Salary (set, limit) (0) | 2021.04.09 |
[LeetCode] Reformat Department Table (sum, case, when, then) (0) | 2021.04.09 |
댓글