SQL

요약값을 보여주는 rollup, grouping sets

에어팟맥스 2022. 7. 5. 17:51

rollup : 합계를 맨 아래에 표시 

cube: 합계를 맨 위에 표시

 

----- >>>>> 요약값(rollup, cube, grouping sets) <<<<< ------
  /*
      1. rollup(a,b,c) 은 grouping sets( (a,b,c),(a,b),(a),() ) 와 같다.
    
         group by rollup(department_id, gender) 은
         group by grouping sets( (department_id, gender), (department_id), () ) 와 같다.
  
      2. cube(a,b,c) 은 grouping sets( (a,b,c),(a,b),(b,c),(a,c),(a),(b),(c),() ) 와 같다.
 
         group by cube(department_id, gender) 은
         group by grouping sets( (department_id, gender), (department_id), (gender), () ) 와 같다.
  */

 

 

--- employees 테이블에서 부서번호별로 인원수를 나타내면서 동시에 전체 인원수도 나타내기

 select department_id 부서번호 
         , count(*) 인원수
    from employees
    group by rollup(department_id) ;
    
    /*
 ------------------
 부서번호 인원수
 ------------------
    10	1
    20	2
    30	6
    40	1
    50	45
    60	5
    70	1
    80	34
    90	3
    100	6
    110	2
   NULL 1
   NULL 107
    
    */
select department_id 부서번호 
         , grouping(department_id) Grouping -- grouping(department_id) 은 결과값이 0,1 만 나온다. 0은 department_id 컬럼의 값으로 그룹을 지었다는 말이고, 1은 그룹을 짓지 않았다는 말이다.
         , count(*) 인원수
    from employees
    group by rollup(department_id) ;
    
    /*
   ---------------------
 부서번호 grouping 인원수
 -----------------------
    10	0	1
    20	0	2
    30	0	6
    40	0	1
    50	0	45
    60	0	5
    70	0	1
    80	0	34
    90	0	3
    100	0	6
    110	0	2
   NULL 0	1
   NULL 1	107
    
    */

 

 

select decode (grouping(department_id), 0, NVL( to_char(department_id), '인턴'), '전체') 부서번호
         , count(*) 인원수
    from employees
    group by rollup(department_id) ;
    
    /*
  -----------------------
  부서번호   인원수
  -----------------------
    10	1
    20	2
    30	6
    40	1
    50	45
    60	5
    70	1
    80	34
    90	3
    100	6
    110	2
    인턴	1
    전체	107
    */

select decode( grouping(gender), 0, gender, '전체') 성별
         , count(*) 인원수
         , TO_CHAR( round( count(*)/(select count(*) from employees) * 100 , 1 ), '990.0') 퍼센티지
    from 
    (
        select case when substr(jubun, 7,1) in ('2','4') then '여' else '남' end as GENDER
        from employees
    ) V
    group by rollup(gender);
    
    /*
    ----------------
    성별 인원수 퍼센티지
    ----------------
     남	56	  52.3
    여	51	  47.7
    전체	107	 100.0
    */

    select decode( grouping(department_id), 0, NVL(to_char(department_id), '인턴'), '전체') 부서번호
         , decode( grouping(gender), 0, gender, '전체') 성별
         , count(*) 인원수
         , TO_CHAR( round( count(*)/(select count(*) from employees) * 100 , 1 ), '990.0') 퍼센티지
    from 
    (
        select case when substr(jubun, 7,1) in ('2','4') then '여' else '남' end as GENDER
             , department_id
        from employees
    ) V
    group by ROLLUP(department_id, gender);
    
    
    /*
    ------------------------------------
    부서번호     성별     인원수    퍼센티지
    ------------------------------------
    ...         ...     ...       ...
    50          남       23       21.5
    50          여       22       20.6
    ...         ...     ...       ...
    전체     	 전체  	 107	   100.0
 
 */

select decode( grouping(department_id), 0, NVL(to_char(department_id), '인턴'), '전체') 부서번호
         , decode( grouping(gender), 0, gender, '전체') 성별
         , count(*) 인원수
         , TO_CHAR( round( count(*)/(select count(*) from employees) * 100 , 1 ), '990.0') 퍼센티지
    from 
    (
    select case when substr(jubun, 7,1) in ('2','4') then '여' else '남' end as GENDER
         , department_id
    from employees
    ) V
    group by ROLLUP(department_id, gender);
    
    
    
    -- 또는 
    
    
    select decode( grouping(department_id), 0, NVL(to_char(department_id), '인턴'), '전체') 부서번호
         , decode( grouping(gender), 0, gender, '전체') 성별
         , count(*) 인원수
         , TO_CHAR( round( count(*)/(select count(*) from employees) * 100 , 1 ), '990.0') 퍼센티지
    from 
    (
    select case when substr(jubun, 7,1) in ('2','4') then '여' else '남' end as GENDER
         , department_id
    from employees
    ) V
    group by GROUPING SETS( (department_id, gender), department_id, () );
    
    /*
    ------------------------------------
    부서번호     성별     인원수    퍼센티지
    ------------------------------------
    ...         ...     ...       ...
    50          남       23       21.5
    50          여       22       20.6
    ...         ...     ...       ...
    전체     	 전체  	 107	   100.0
 
 */

 

 

'SQL' 카테고리의 다른 글

having 그룹함수조건절  (0) 2022.07.07
요약값을 보여주는 cube  (0) 2022.07.07
그룹 함수 - group by 절  (0) 2022.07.05
그룹 함수(집계 함수)  (0) 2022.07.05
VIEW - inline view, stored view응용(대출 이자 납부액 구하기)  (0) 2022.07.05