2011년 7월 5일 화요일

날짜별로 가로로 나오게 Query 만들기


SELECT a1.*,
       CASE
         WHEN ROW_NUMBER() OVER(PARTITION BY a1.jisa_cd
        ORDER BY a1.jisa_cd)=1 THEN COUNT(*) OVER(PARTITION BY a1.jisa_cd)
         ELSE 0
       END rowspan_cnt
FROM   (SELECT jisa_cd,
               center_cd,
               DECODE(jisa_cd, '000', '총합계', jisa_nm) jisa_nm,
               DECODE(center_cd, 'ZZZZ', '합계', center_nm) center_nm,
               ( date2009 + date2010 + date2011 ) sum_all,
               ( dateant2009 + dateant2010 + dateant2011 ) sumamt_all,
               date2009,
               date2010,
               date2011,
               dateant2009,
               dateant2010,
               dateant2011
        FROM   (SELECT NVL(jisa_cd, '000') jisa_cd,
                       NVL(center_cd, 'ZZZZ') center_cd,
                       MAX(jisa_nm) jisa_nm,
                       MAX(center_nm) center_nm,
                       SUM(date2009) date2009,
                       SUM(date2010) date2010,
                       SUM(date2011) date2011,
                       SUM(dateant2009) dateant2009,
                       SUM(dateant2010) dateant2010,
                       SUM(dateant2011) dateant2011
                FROM   (SELECT a.jisa_cd,
                               a.center_cd,
                               b.jisa_nm,
                               c.center_nm,
                               a.expect_amt,
                               DECODE(TO_CHAR(a.create_dt,'YYYY'), '2009', 1, 0) date2009,
                               DECODE(TO_CHAR(a.create_dt,'YYYY'), '2010', 1, 0) date2010,
                               DECODE(TO_CHAR(a.create_dt,'YYYY'), '2011', 1, 0) date2011,
                               DECODE(TO_CHAR(a.create_dt,'YYYY'), '2009', expect_amt, 0) dateant2009,
                               DECODE(TO_CHAR(a.create_dt,'YYYY'), '2010', expect_amt, 0) dateant2010,
                               DECODE(TO_CHAR(a.create_dt,'YYYY'), '2011', expect_amt, 0) dateant2011
                        FROM   (SELECT jisa_cd,
                                       center_cd,
                                       create_dt,
                                       expect_amt
                                FROM   qis_pl_receipt
                                WHERE  1 = 1
                                AND    TO_CHAR(create_dt, 'YYYYMMDD') >= '20090000'
                                AND    TO_CHAR(create_dt, 'YYYYMMDD') <= '20119999'
                                --AND 검색조건
                                       ) a,
                               qis_jisa_mst b,
                               qis_center_mst c
                        WHERE  1 = 1
                        AND    a.jisa_cd = b.jisa_cd
                        AND    a.center_cd = c.center_cd )
                GROUP BY ROLLUP(jisa_cd, center_cd) )
        ORDER BY jisa_cd, center_cd) a1
  WHERE 1 = 1

댓글 없음:

댓글 쓰기