샘플 데이터 생성
create table sales (
product_id varchar(5),
month date,
revenue numeric
);
insert into sales
select 'P'||LPAD(cast(row_number() over() as text), 3, '0')
, DATE '2020-01-01' + interval '1 month' * ((random()*10)::integer)
, cast(random()*100000 as integer)
from generate_series(1,100);
select *
from sales ;
![](https://velog.velcdn.com/images/dafld/post/0cde3793-3513-4505-8905-302dd74ae2af/image.png)
Group by
group by
select product_id, month, sum(revenue)
from sales
group by product_id, month
order by product_id;
![](https://velog.velcdn.com/images/dafld/post/9d5ef4f6-df04-4c7a-8449-48e6c8b2ca65/image.png)
group by rollup
select product_id, month, sum(revenue)
from sales
group by rollup(product_id, month)
order by product_id, month;
![](https://velog.velcdn.com/images/dafld/post/0e3509a2-e793-4f87-9348-3124c1361607/image.png)
group by cube
select product_id, month, sum(revenue)
from sales
group by cube(product_id, month)
order by product_id, month;
![](https://velog.velcdn.com/images/dafld/post/eca6ed16-e9c2-4d15-8c96-b7c4afe2a713/image.png)