Contents

Consecutive Numbers(Window함수)

   Apr 1, 2023     1 min read

Consecutive Numbers

문제

180-1.jpg
180-2.jpg

코드

Window함수 사용

MS SQL Server/Oracle

SELECT DISTINCT L.NUM AS ConsecutiveNums
FROM (
     SELECT NUM
          , LEAD(NUM, 1) OVER (ORDER BY ID) AS NEXT
          , LEAD(NUM, 2) OVER (ORDER BY ID) AS AFTERNEXT
     FROM LOGS
) L
WHERE L.NUM = L.NEXT AND L.NUM = L.AFTERNEXT

Window함수 미사용

Oracle

SELECT DISTINCT L.NUM AS ConsecutiveNums
FROM LOGS L
    INNER JOIN LOGS F ON L.ID + 1 = F.ID
    INNER JOIN LOGS S ON L.ID + 2 = S.ID
WHERE L.NUM = F.NUM 
    AND L.NUM = S.NUM

수행결과

180result.jpg
180result1.jpg