sql

Named Window

임이레 2024. 8. 13. 14:45

https://dev.mysql.com/doc/refman/8.0/en/window-functions-named-windows.html 

 

MySQL :: MySQL 8.0 Reference Manual :: 14.20.4 Named Windows

Windows can be defined and given names by which to refer to them in OVER clauses. To do this, use a WINDOW clause. If present in a query, the WINDOW clause falls between the positions of the HAVING and ORDER BY clauses, and has this syntax: WINDOW window_n

dev.mysql.com

https://dev.mysql.com/doc/refman/8.0/en/window-functions-frames.html

 

MySQL :: MySQL 8.0 Reference Manual :: 14.20.3 Window Function Frame Specification

14.20.3 Window Function Frame Specification The definition of a window used with a window function can include a frame clause. A frame is a subset of the current partition and the frame clause specifies how to define the subset. Frames are determined with

dev.mysql.com

| 공식문서 

 

예시 

SELECT time_at
	, column1 
    , RANK() OVER (ORDER BY column1 DESC) AS rk
    , DENSE_RANK() OVER (ORDER BY column1 DESC) AS dense_rk
    , ROW_NUMBER() OVER (ORDER BY column1 DESC) AS row_n
FROM table1 
ORDER BY rk
이 쿼리에서 중복적으로 윈도우 함수를 호출하고 있다 .

 

 

이 중복되는 부분을 WINDOW 윈도우이름 AS (윈도우스펙) 형식으로 정의를 한 후 윈도우 이름을 호출하여 유용하게 사용할 수 있다! 

SELECT time_at
	, column1 
    , RANK() OVER wd AS rk
    , DENSE_RANK() OVER wd AS dense_rk
    , ROW_NUMBER() OVER wd AS row_n
FROM table1 

-- 윈도우 형식 호출하기 위한 형식을 작성--
WINDOW wd AS (ORDER BY column1 DESC)
ORDER BY rk

'sql' 카테고리의 다른 글

사용자 정의 함수  (0) 2025.04.28
Frame  (0) 2024.08.13
RANK 순위 함수  (0) 2024.08.12
집계함수(윈도우 함수)  (0) 2024.08.12
ROW_NUMBER()  (0) 2024.07.10