Window절
요약
- 어떻게 윈도우의 크기(시작점, 종료점)을 정할 것이냐
windowing clause =
{rows | range}
{between}
{unbounded preceding | current row | value_expr {preceding | following }}
and
{unbounded following | current row | value_expr {preceding | following}}
|
{unbounded preceding | current row | value_expr preceding}
Window 절의 상세 구문 설명
- order by 가 있으면, default는 ***range between unbounded preceding and current row
= 해당 partition의 제일 위부터 현재 row까지 계산함 - between이 없고 시작범위만 주어지면 종료범위의 default는 current row
- ex : rows between unbounded preceding and unbounded following - 처음부터 끝까지
- row는 말그대로 row를 기준으로 한다고 치면, range는 날짜의 경우 row와 관계없이 범위를 지정할 수 있다는 특징이 있다.(가격범위, 날짜범위 등)
구문 | 구문 설명 |
---|---|
rows / range | window의 개별 row를 정의함. row는 물리적인 row를, range는 논리적인 row를 의미. order by 절이 없으면 해당 구문은 기술할 수 없음. - over (partition by category_id order by unit_price rows between unbounded preceding and current row) - over (partition by customer_id order by order_date range between interval ‘2’ day preceding and current row) |
between … and | window의 시작과 종료지점을 기술. between 다음이 시작지점, and 다음이 종료지점 between이 없다면 row/range 다음이 시작점, (기본설정으로) 현재 row(current row)가 종료점으로 설정 - over (partition by categor_id order by unit_price rows between 2 preceding and current row) - over (partition by category_id order by unit_price rows 2 preceding) |
unbounded preceding | window의 시작이 partition의 첫번째 row부터 시작함을 기술. window의 종료점으로는 사용될 수 없음. |
unbounded following | window의 종료가 partition의 마지막 row에서 종료됨을 기술. window의 시작점으로는 사용될 수 없음. |
current row | window의 시작점 또는 종료점으로 사용될 수 있으나, 보통은 종료점으로 사용 종료점으로 사용 시 window의 종료가 현재 row에서 종료됨을 기술 시작점으로 사용 시 window의 시작이 현재 row에서 시작됨을 기술 over (partition by category_id order by unit_price rows between unbounded preceding and current row) over (partition by category_id order by unit_price rows between current row and unbounded following) |
rows + value 표현 preceding or value 표현 following |
2 preceding 또는 3 following과 같이 value 표현 preceding/following을 지정하여 특정 지점을 window의 시작 또는 종료지점으로 나타낼 수 있음. rows나 range 모두와 함께 사용될 수 있으며, rows와 사용될 때는 물리적인 row위치를 지정 - over (partition by category_id order by unit_price rows between 2 preceding and current row) - over (partition by category_id order by unit_price rows between 2 preceding and 1 following) - over (partition by category_id order by unit_price rows between and 1 following and current row) |
range + value 표현 preceding or value 표현 following |
마찬가지로 특정 지점을 window의 시작 또는 종료지점으로 나타낼 수 있음. range와 사용할 때는 논리적인 row위치를 지정하므로 value표현이 단순한 숫자외에도 논리적인 값을 적용해야 함. 보통은 숫자값과 interval 값이 사용됨. 또한 order by 절의 컬럼도 numeric 또는 (대부분) date/timestamp 가 되어야 함. - over (partition by customer_id order by order_date range between interval ‘2’ day preceding and current row) |
마치며
일단 오늘을 마지막으로 SQL fundamentals 강의 수강은 중지하기로 결정했다.
그 이유는 지금 당장 활용해야하는 데이터분석 차원에서는 아는 지식으로 충분하다는 판단이 들었기 때문이다.
계속 이러다가 SQL 뇌절각 나옴…
만일 권철민님의 후속강의가 나오게 된다면 그 때 빠르게 리뷰한 다음 쫓아가볼까 하는 생각이 든다. 그럼 이만 아디오스!
댓글남기기