메뉴 건너뛰기

조회 수 11925 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

오라클 GROUP BY 쿼리에서 ROLLUP 함수를 사용하여 손쉽게 합계와 소계를 구할 수 있다.

 

1. GROUP BY 컬럼이 하나인 경우

   : GROUP BY 절에 ROLLUP 함수를 추가한 후 그룹핑 컴럼을 인자로 넣는다.

 

-- 가상 테이블

WITH TEST_TABLE AS (

   SELECT 'SMITH' NM, 'CLERK' JOB, 800 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'ALLEN' NM, 'SALESMAN' JOB, 1600 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'WARD' NM, 'SALESMAN' JOB, 1250 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'JONES' NM, 'MANAGER' JOB, 2975 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'MARTIN' NM, 'SALESMAN' JOB, 1250 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'BLAKE' NM, 'MANAGER' JOB, 2850 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'CLARK' NM, 'MANAGER' JOB, 2450 SAL, 'ACCOUNTING' DEPT FROM DUAL UNION ALL

   SELECT 'SCOTT' NM, 'ANALYST' JOB, 3000 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'KING' NM, 'PRESIDENT' JOB, 5000 SAL, 'ACCOUNTING' DEPT FROM DUAL UNION ALL

   SELECT 'TURNER' NM, 'SALESMAN' JOB, 1500 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'ADAMS' NM, 'CLERK' JOB, 1100 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'JAMES' NM, 'CLERK' JOB, 950 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'FORD' NM, 'ANALYST' JOB, 3000 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'MILLER' NM, 'CLERK' JOB, 1300 SAL, 'ACCOUNTING' DEPT FROM DUAL

)

 

SELECT JOB

           , SUM(SAL)

   FROM TEST_TABLE

 GROUP BY ROLLUP(JOB)

 

 

 

2. GROUP BY 컬럼이 두 개 이상인 경우

   : 합계 및 소계 까지 계산되어 표시된다.

 

-- 가상 테이블

WITH TEST_TABLE AS (

   SELECT 'SMITH' NM, 'CLERK' JOB, 800 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'ALLEN' NM, 'SALESMAN' JOB, 1600 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'WARD' NM, 'SALESMAN' JOB, 1250 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'JONES' NM, 'MANAGER' JOB, 2975 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'MARTIN' NM, 'SALESMAN' JOB, 1250 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'BLAKE' NM, 'MANAGER' JOB, 2850 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'CLARK' NM, 'MANAGER' JOB, 2450 SAL, 'ACCOUNTING' DEPT FROM DUAL UNION ALL

   SELECT 'SCOTT' NM, 'ANALYST' JOB, 3000 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'KING' NM, 'PRESIDENT' JOB, 5000 SAL, 'ACCOUNTING' DEPT FROM DUAL UNION ALL

   SELECT 'TURNER' NM, 'SALESMAN' JOB, 1500 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'ADAMS' NM, 'CLERK' JOB, 1100 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'JAMES' NM, 'CLERK' JOB, 950 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'FORD' NM, 'ANALYST' JOB, 3000 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'MILLER' NM, 'CLERK' JOB, 1300 SAL, 'ACCOUNTING' DEPT FROM DUAL

)

 

SELECT JOB

          , DEPT

          , SUM(SAL)

   FROM TEST_TABLE

 GROUP BY ROLLUP(JOB, DEPT)

 

 

 

3. GROUP BY 컬럼이 두 개 이상인 경우 합계만 표시하고 싶을 때

   : HAVING 절에 GROUPING_ID 함수를 이용하여 소계 값을 제외할 수 있다.

 

-- 가상 테이블

WITH TEST_TABLE AS (

   SELECT 'SMITH' NM, 'CLERK' JOB, 800 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'ALLEN' NM, 'SALESMAN' JOB, 1600 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'WARD' NM, 'SALESMAN' JOB, 1250 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'JONES' NM, 'MANAGER' JOB, 2975 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'MARTIN' NM, 'SALESMAN' JOB, 1250 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'BLAKE' NM, 'MANAGER' JOB, 2850 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'CLARK' NM, 'MANAGER' JOB, 2450 SAL, 'ACCOUNTING' DEPT FROM DUAL UNION ALL

   SELECT 'SCOTT' NM, 'ANALYST' JOB, 3000 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'KING' NM, 'PRESIDENT' JOB, 5000 SAL, 'ACCOUNTING' DEPT FROM DUAL UNION ALL

   SELECT 'TURNER' NM, 'SALESMAN' JOB, 1500 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'ADAMS' NM, 'CLERK' JOB, 1100 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'JAMES' NM, 'CLERK' JOB, 950 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'FORD' NM, 'ANALYST' JOB, 3000 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'MILLER' NM, 'CLERK' JOB, 1300 SAL, 'ACCOUNTING' DEPT FROM DUAL

)

 

SELECT JOB

           , DEPT

           , SUM(SAL)

   FROM TEST_TABLE

 GROUP BY ROLLUP(JOB, DEPT) HAVING GROUPING_ID(JOB, DEPT) IN (0, 3)

 

 

 

4. ROLLUP 컬럼에 합계 표시하기

   : DECODE 함수를 이용하여 합계 컬럼 값을 NULL 대신 합계로 표시할 수 있다.

 

-- 가상 테이블

WITH TEST_TABLE AS (

   SELECT 'SMITH' NM, 'CLERK' JOB, 800 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'ALLEN' NM, 'SALESMAN' JOB, 1600 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'WARD' NM, 'SALESMAN' JOB, 1250 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'JONES' NM, 'MANAGER' JOB, 2975 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'MARTIN' NM, 'SALESMAN' JOB, 1250 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'BLAKE' NM, 'MANAGER' JOB, 2850 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'CLARK' NM, 'MANAGER' JOB, 2450 SAL, 'ACCOUNTING' DEPT FROM DUAL UNION ALL

   SELECT 'SCOTT' NM, 'ANALYST' JOB, 3000 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'KING' NM, 'PRESIDENT' JOB, 5000 SAL, 'ACCOUNTING' DEPT FROM DUAL UNION ALL

   SELECT 'TURNER' NM, 'SALESMAN' JOB, 1500 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'ADAMS' NM, 'CLERK' JOB, 1100 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'JAMES' NM, 'CLERK' JOB, 950 SAL, 'SALES' DEPT FROM DUAL UNION ALL

   SELECT 'FORD' NM, 'ANALYST' JOB, 3000 SAL, 'RESEARCH' DEPT FROM DUAL UNION ALL

   SELECT 'MILLER' NM, 'CLERK' JOB, 1300 SAL, 'ACCOUNTING' DEPT FROM DUAL

)

 

SELECT DECODE(JOB, NULL, '합계', JOB) JOB

           , DEPT

           , SUM(SAL)

    FROM TEST_TABLE

  GROUP BY ROLLUP(JOB, DEPT) HAVING GROUPING_ID(JOB, DEPT) IN (0, 3)

 

 


  1. 오라클 10, 16진수(Decimal, Hex) 변환

    Date2018.10.27 Views4400
    Read More
  2. 오라클 랜덤함수

    Date2018.10.27 Views8000
    Read More
  3. 오늘을 기준으로 해당주(week)의 모든 일자조회

    Date2017.01.20 Views8399
    Read More
  4. 오라클] 숫자 체크 방법 (IS_NUMBER, IS_NUMERIC)

    Date2017.01.20 Views8833
    Read More
  5. 오라클 ORDER BY : 정렬

    Date2017.01.20 Views8299
    Read More
  6. 오라클 NULL : 값이 존재하지 않는 상태

    Date2017.01.20 Views7269
    Read More
  7. CentOS 6.5 에 Oracle Database 11g Release 2 설치하기

    Date2016.12.08 Views10915
    Read More
  8. RMAN 을 이용한 오라클 백업

    Date2016.12.08 Views8632
    Read More
  9. [Oracle] Rownum을 이용한 페이징 처리

    Date2016.12.08 Views12297
    Read More
  10. [Oracle] 테이블 스페이스 및 사용자 계정 생성 방법

    Date2016.12.08 Views8653
    Read More
  11. [Oracle] DB Export, Import 방법

    Date2016.12.08 Views12678
    Read More
  12. 오라클 백업 및 복구(Export, Import)

    Date2016.12.08 Views9835
    Read More
  13. 오라클 DB 백업과 복원

    Date2016.12.08 Views9988
    Read More
  14. 데이터베이스 백업하기 ( import : 가져오기 , export : 내보내기 )

    Date2016.12.08 Views9951
    Read More
  15. 컬럼의 값 만큼 행(Row)을 늘리기

    Date2016.12.08 Views20388
    Read More
  16. ROLLUP 합계, 소계 구하기 (GROUP BY)

    Date2016.12.08 Views11925
    Read More
  17. 여러개(다중) LIKE 검색 방법 (REGEXP_LIKE 함수)

    Date2016.12.08 Views12897
    Read More
  18. 숫자를 문자로 변환 시 소수점 처리 (TO_CHAR, FM)

    Date2016.12.08 Views15091
    Read More
  19. PL/SQL에서 자바(Java) 클래스(Class), 함수 실행 방법

    Date2016.12.08 Views9449
    Read More
  20. 숫자 체크 방법 (IS_NUMBER, IS_NUMERIC)

    Date2016.12.08 Views23247
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 Next
/ 7

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved