2011년 6월 2일 목요일

테이블 rowspan 갯수 구하기


SELECT fail_sect_cd, fail_detail_cd,
       CASE WHEN ROW_NUMBER() OVER(PARTITION BY fail_sect_cd ORDER BY fail_sect_cd)=1 THEN COUNT(*) OVER(PARTITION BY fail_sect_cd) ELSE 0 END rowspan_cnt, -- table rowspan을 만들기 위해서 lookup_code 갯수 구함
       (t_20110118 + t_20110119 + t_20110120) sum_all ,
       t_20110118 ,
       t_20110119 ,
       t_20110120
FROM   (SELECT NVL(fail_sect_cd, '00') fail_sect_cd, NVL(fail_detail_cd, '0000') fail_detail_cd ,
               SUM(t_20110118) t_20110118 ,
               SUM(t_20110119) t_20110119 ,
               SUM(t_20110120) t_20110120
        FROM   (SELECT actual_return_date ,
                       fail_sect_cd, fail_detail_cd ,
                       DECODE(actual_return_date, '20110118', qty, 0) t_20110118 ,
                       DECODE(actual_return_date, '20110119', qty, 0) t_20110119 ,
                       DECODE(actual_return_date, '20110120', qty, 0) t_20110120
                FROM   (SELECT *
                        FROM   통계테이블
                        WHERE  1 = 1
                        AND    actual_return_date >= '20110118'
                        AND    actual_return_date <= '20110120'
                        -- 검색조건 추가
                        )
                WHERE  1 = 1 )
        GROUP BY ROLLUP(fail_sect_cd, fail_detail_cd)
        )
        ORDER BY fail_sect_cd, fail_detail_cd

댓글 없음:

댓글 쓰기