Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the ultimate-member domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /ledcorps/www/wp/wp-includes/functions.php on line 6114 2.5 GROUPING SETS – 대나무숲

2.5 GROUPING SETS

출처 http://www.gurubee.net/lecture/2680

GROUPING SETS

  • – GROUPING SETS 함수는 GROUP BY의 확장된 형태로 하나의 GROUP BY절에 여러개의 그룹 조건을 기술할 수 있다.
  • – GROUPING SETS 함수의 결과는 각 그룹 조건에 대해 별도로 GROUP BY한 결과를 UNION ALL한 결과와 동일하다.
  • – GROUPING SETS 함수를 사용하면 UNION ALL등을 사용하여 복잡하게 SQL문장을 작성했던 것을 간단하게 한 문장으로 해결 할 수 있다.

아래는 GROUPING SETS함수와 GROUP BY, UNION ALL을 이용하여 동일한 결과를 출력하는 예제이다.

--  GROUPING SETS 예제
SELECT deptno, job, SUM(sal)
 FROM emp
 GROUP BY GROUPING SETS(deptno, job);
 
 
-- GROUP BY와 UNION ALL을 이용한 예제
SELECT NULL deptno,  job, SUM(sal)
  FROM emp
 GROUP BY job
 UNION ALL
SELECT deptno, NULL job, SUM(sal)
  FROM emp
 GROUP BY deptno;
 
 
-- 조회결과
   DEPTNO JOB                  SUM(SAL)
--------- ------------------ ----------
          CLERK                    4150
          SALESMAN                 5600
          PRESIDENT                5000
          MANAGER                  8275
          ANALYST                  6000
       30                          9400
       20                         10875
       10                          8750

답글 남기기 0

Your email address will not be published. Required fields are marked *