[PostgreSQL] group by 함수 (rollup, cube)

Ja L·2024년 3월 29일
0

[PostgreSQL] Operation

목록 보기
32/39

샘플 데이터 생성


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 ;

Group by

group by

--standard group by 
 select product_id, month, sum(revenue)
   from sales 
 group by product_id, month
 order by product_id;

group by rollup

-- rollup
 select product_id, month, sum(revenue)
   from sales 
 group by rollup(product_id, month)
order by product_id, month;

group by cube

--cube
 select product_id, month, sum(revenue)
   from sales 
 group by cube(product_id, month)
 order by product_id, month;

profile
DB Engineer

0개의 댓글

관련 채용 정보