안녕하세요. 오늘 배운 내용 중 제일 새로웠던 개념입니다.
Window Function 은 각 행의 관계를 정의하기 위한 함수로 그룹 내의 연산을 쉽게 만들어줍니다. 자체적으로 제공해주는 기능을 이용하면 조금 더 편리한데 바로 이 기능들이 Window function 으로 제공되고 있습니다.
🍄 Window Function 의 기본 구조
SELECT cuisine_type,
restaurant_name,
cnt_order,
ranking
FROM
(
select cuisine_type,
restaurant_name,
cnt_order,
rank() over(partition by cuisine_type order by cnt_order desc) ranking
from
(
select cuisine_type,
restaurant_name,
count(1) cnt_order
from food_orders
group by 1, 2
)a
)b
where ranking <= 3

select cuisine_type,
restaurant_name,
order_count,
sum(order_count) over (partition by cuisine_type) sum_cuisine_type,
sum(order_count) over (partition by cuisine_type order by order_count, restaurant_name) cumulative_sum
from
(
select cuisine_type, restaurant_name, count(1) order_count
from food_orders
group by 1, 2
) a

sum(이것의 합계) over(partition by 묶어줄 곳 order by 정렬기준)
sum(order_count) over (partition by cuisine_type) sum_cuisine_type
카테고리별 합
sum(order_count) over (partition by cuisine_type order by order_count, restaurant_name) cumulative_sum
카테고리별 누적합