Pivot Table : 2개 이상의 기준으로 집계할 때 보기 쉽게 보여주는 것을 의미한다.
select restaurant_name,
max(if(hh='15', cnt_order, 0)) "15",
max(if(hh='16', cnt_order, 0)) "16",
max(if(hh='17', cnt_order, 0)) "17",
max(if(hh='18', cnt_order, 0)) "18",
max(if(hh='19', cnt_order, 0)) "19",
max(if(hh='20', cnt_order, 0)) "20"
from
(
select a.restaurant_name,
substring(b.time, 1, 2) hh,
count(1) cnt_order
from food_orders a inner join payments b on a.order_id=b.order_id
where substring(b.time, 1, 2) between 15 and 20
group by 1, 2
) a
group by 1
order by 7 desc
max함수 안에 조건문을 넣어 만든다.
Window Function
:Window Function 은 각 행의 관계를 정의하기 위한 함수로 그룹 내의 연산을 쉽게 만들어줍니다. over과 함께 사용
rank() over() 예시
select cuisine_type,
restaurant_name,
rank() over (partition by cuisine_type order by order_count desc) rn,
order_count
from
(
select cuisine_type, restaurant_name, count(1) order_count
from food_orders
group by 1, 2
) a
sum() over() 예시
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
'TIL' 카테고리의 다른 글
(2024-05-02) 팀 프로젝트 템플릿코드 분석및 구현 (0) | 2024.05.02 |
---|---|
(2024-05-01)자바 개인 과제 리팩토링 (1) | 2024.05.01 |
(2024-04-29) SQL IF,CASE,JOIN (0) | 2024.04.29 |
(2024-04-26) 자바 개인 과제 level2 (0) | 2024.04.26 |
(2024-04-25) 자바 개인과제 level 1 (1) | 2024.04.25 |