나의 분석일기 ♬
[SQL] Analytic SQL_Window 절 본문
[SQL] Analytic SQL_Window 절
Window 절 =
{ 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 절의 상세 구문 설명
| 구문 | 구문 설명 |
| ROWS | RANGE | Window의 개별 row를 정의함. Rows는 물리적인 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 category_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 표현 PRECEDNIG or value 표현 FOLLOWNG |
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 표현 PRECEDNIG or value 표현 FOLLOWNG |
마찬가지로 특정 지점을 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)
|
'Tools > SQL' 카테고리의 다른 글
| [SQL] Analytic SQL_집계 Analytic (0) | 2024.05.09 |
|---|---|
| SQLD 개념 정리 1 (0) | 2023.11.02 |
| [SQL] 역분위함수 - percentile_disc() / percentile_cont() (0) | 2023.03.21 |
| [SQL] 순위 함수 - cume_dist, percent_rank, ntile (0) | 2023.03.21 |
| [SQL] first_value() / last_value() 함수 (0) | 2023.03.21 |
Comments