Post

SQL Grammer (10) - Pivot tabel, Window function

SQL Grammer (10) - Pivot tabel, Window function




Pivot tabel


2개 이상의 기준으로 데이터를 집계할 때, 보기 쉽게 배열하여 보여주는 것을 의미합니다. 집계 기준을 행의 기준으로 잡고, 구분 컬럼으로 데이터를 보여줄 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;

이렇게 하면 음식점 별, 시간대별 주문 건수를 한눈에 확인할 수 있게 데이터를 조회할 수 있습니다.

restaurant_name151617181920
Shake Shack1376939
The Meatball Shop427349
Blue Ribbon Fried Chicken542445




Window function


각 행의 관계를 정의하기 위한 함수로, 그룹 내의 연산을 쉽게 만들어주는 함수입니다. 예를 들어 한식 식당 중에서 주문건수가 많은 순으로 순위를 매기고 싶은 경우에 사용할 수 있습니다.

기본 구조는 window_function(argument) over (partition by 기준 칼럼, order by 정렬 기준)의 형태로 쓰입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
select cuisine_type,  
       restaurant_name,  
       order_count,  
       rn "순위"  
from  
(  
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  
) b  
where rn<=3  
order by 1, 4;

Rank()를 사용해 음식의 타입별로 레스토랑들의 주문건수 순위를 볼 수 있습니다.

cuisine_typerestaurant_nameorder_count순위
AmericanShake Shack2191
AmericanBlue Ribbon Fried Chicken962
AmericanFive Guys Burgers and Fries293
ChineseRedFarm Broadway591
ChineseRedFarm Hudson552
ChineseHan Dynasty463