이유's Programming/DBMS

[ 3- 4] 그룹 소계함수 - 보고서 작성을 훨씬 쉽고 매끄럽게! group by 와 order by 사용 / rollup 과 order by 함수

살아가는 이유_EU 2021. 4. 22. 10:45
728x90
반응형

우선 두 가지 방법이 있는데 먼저 group by 의 사용을 먼저 보자. 

Products table 에 group by 를 이용해서 하는 경우를 확인해보자!

 

1) group by 케이스 

SELECT count(*), categoryid FROM Products
group by CategoryID

 

 

2) Roll up 케이스 

- ROLLUP 의 특징 : 아래 처럼 이렇게 1개의 그룹에 대해서 하는 것이 아니라 여러 그룹 즉 소그룹 간의 데이터를 하는 것이기 때문에 아래와 같은 예시는 쓰이지 않는다. 

SELECT count(*), categoryid FROM Products
group by ROLLUP ( categoryid)
order by category id 

 

단 다양한 GROUP 에 대해서 쓰이는데 

select b.dname, a.job, count (*) as emp_cnt, sum ( a.sal) as sal_sum
from emp a, dept b
where b.deptno = a.deptno
group by rollup ( b.dname, a.job)
order by b.dname, a.job

 

728x90
반응형