본문 바로가기
프로그래밍/SQL, Hive, SAS 관련 정보

[SQL - Frequently Used Code] partition by / order by 없이 window함수 쓰기

by TrillionNT 2025. 4. 7.

2025.02.24 - [프로그래밍/SQL, Hive, SAS 관련 정보] - [SQL] Table of Contents


https://leetcode.com/problems/consecutive-numbers/?envType=study-plan-v2&envId=top-sql-50

 

window함수의 partition by와 order by는 필수로 넣어야 하는 옵션은 아닙니다. 가끔은 둘 다 필요 없을 때가 있죠. 이럴 때는 over() 만 해주면 됩니다. 

select distinct(num) ConsecutiveNums
from 
    (select id, num, lag(num, 1) over() l1, lag(num, 2) over() l2
    from Logs) tt
where num=l1 and num=l2;