[답변]쿼리 질문이요... 0 0 2,471

by 성시현 [2007.11.06 15:34:35]


ROLLUP으로도 만들면야 되겠지만 간단히 CUBE로 만들어 봤습니다.

 

-- 테스트데이터...

WITH t AS
 ( 
 SELECT '20071111' AS dday, '001' AS jno, trim(' 엘지') AS ccomp, trim('  엘지') AS mcomp,  111111 AS cno, trim(' 00') AS hmon,   10000 AS amt FROM dual UNION ALL
 SELECT '20071111' AS dday, '002' AS jno, trim(' 신한') AS ccomp, trim('  신한') AS mcomp,  222222 AS cno, trim(' 00') AS hmon,   20000 AS amt FROM dual UNION ALL
 SELECT '20071112' AS dday, '001' AS jno, trim('비씨A') AS ccomp, trim(' 비씨 ') AS mcomp, 333333  AS cno, trim('00 ') AS hmon,  20000  AS amt FROM dual UNION ALL
 SELECT '20071112' AS dday, '002' AS jno, trim('비씨B') AS ccomp, trim(' 비씨 ') AS mcomp, 444444  AS cno, trim('00 ') AS hmon,  20000  AS amt FROM dual UNION ALL
 SELECT '20071112' AS dday, '003' AS jno, trim(' 시티') AS ccomp, trim('  시티') AS mcomp,  555555 AS cno, trim(' 00') AS hmon,   20000 AS amt FROM dual UNION ALL
 SELECT '20071113' AS dday, '001' AS jno, trim(' 엘지') AS ccomp, trim('  엘지') AS mcomp,  666666 AS cno, trim(' 00') AS hmon,   20000 AS amt FROM dual UNION ALL
 SELECT '20071113' AS dday, '002' AS jno, trim(' 엘지') AS ccomp, trim('  엘지') AS mcomp,  777777 AS cno, trim(' 00') AS hmon,   20000 AS amt FROM dual UNION ALL
 SELECT '20071113' AS dday, '003' AS jno, trim(' 신한') AS ccomp, trim('  신한') AS mcomp,  888888 AS cno, trim(' 00') AS hmon,   20000 AS amt FROM dual UNION ALL
 SELECT '20071113' AS dday, '004' AS jno, trim(' 신한') AS ccomp, trim('  신한') AS mcomp,  999999 AS cno, trim(' 00') AS hmon,   20000 AS amt FROM dual
 )

 

-- 쿼리 ...

SELECT
   DECODE(GROUPING(dday) || GROUPING(mcomp) || GROUPING(jno)
     , '011', '일별소계'
   , '101', '매입사별 총계 :' || mcomp
   , '111', '총계', dday) AS 일자
 , jno, ccomp, mcomp, cno, hmon
 , SUM(amt) AS amt
FROM t
GROUP BY CUBE(dday, mcomp, (jno, ccomp, cno, hmon))
HAVING GROUPING(dday) || GROUPING(mcomp) || GROUPING(jno) = '000' -- raw data
    OR GROUPING(dday) || GROUPING(mcomp) || GROUPING(jno) = '011' -- 일자별계
    OR GROUPING(dday) || GROUPING(mcomp) || GROUPING(jno) = '101' -- 매입사계
    OR GROUPING(dday) || GROUPING(mcomp) || GROUPING(jno) = '111' -- 총계
ORDER BY dday, GROUPING(dday), jno, GROUPING(jno)

 

-- 결과...

일자        JNO CCOMP   MCOMP   CNO   HMON  AMT
20071111    001 엘지    엘지    111111  00  10000
20071111    002 신한    신한    222222  00  20000
일별소계                                             30000
20071112    001 비씨A   비씨    333333  00  20000
20071112    002 비씨B   비씨    444444  00  20000
20071112    003 시티    시티    555555  00  20000
일별소계                                             60000
20071113    001 엘지    엘지    666666  00  20000
20071113    002 엘지    엘지    777777  00  20000
20071113    003 신한    신한    888888  00  20000
20071113    004 신한    신한    999999  00  20000
일별소계                                             80000
매입사별 총계 :비씨     비씨                40000
매입사별 총계 :시티     시티                20000
매입사별 총계 :신한     신한                60000
매입사별 총계 :엘지     엘지                50000
총계                                       170000

건승하시길...수고하세요~~

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입