본문 바로가기

development/oracle

오라클 열을 행으로

dict라는 테이블은 오라클에서 기본적으로 제공하는 테이블이며
rownum에 따라 보여줄 행수에 맞는 필드를 명시하면 된다.

사용방법
    SELECT
         DECODE(ROWNUM, 1, 필드명A, 2, 필드명B)
    FROM
         대상테이블,
        (SELECT ROWNUM FROM DICT WHERE ROWNUM <= 행)


사용 예(사용 예가 너무 길어서 폰트를 좀 줄였으니...복사-붙여넣기로 보세요)
SELECT
    DECODE(rownum, 1, '기성금',     2, '선급금',        3, '부가세',        4, '소계',          5, '합계',        6, '부가세구분') as gbn_nm,
    DECODE(rownum, 1, a.lastAmt,   2, a.lastPreAmt,   3, a.sumLastAmt,   4, a.vatAmt,       5, totalLastAmt, 6, '') as last,
    DECODE(rownum, 1, a.saidAmt,   2, a.saidPreAmt,   3, a.sumSaidAmt,   4, a.saidVatAmt,   5, totalSaidAmt, 6, TAX_YN) as said,
    DECODE(rownum, 1, a.sumAmt,    2, a.sumPreAmt,    3, a.sumAllAmt,    4, a.sumAllVatAmt, 5, totalAmt,     6, '') as sum
FROM   
(SELECT
    LAST_AMT as lastAmt,                          
 SAID_AMT as saidAmt,              
 LAST_AMT + SAID_AMT as sumAmt,
    BFMM_PRE_AMT_ACUM as lastPreAmt,                 
 SAID_PREAMT as saidPreAmt,           
 BFMM_PRE_AMT_ACUM + SAID_PREAMT as sumPreAmt,
    LAST_AMT + BFMM_PRE_AMT_ACUM as sumLastAmt,      
 SAID_AMT + SAID_PREAMT as sumSaidAmt,
 LAST_AMT + BFMM_PRE_AMT_ACUM + SAID_AMT+SAID_PREAMT as sumAllAmt,
    LAST_VAT + BFMM_PRE_AMT_ACUM_VAT as vatAmt,  
 SAID_VAT + SAID_PREVAT as saidVatAmt,
 LAST_VAT + BFMM_PRE_AMT_ACUM_VAT + SAID_VAT + SAID_PREVAT as sumAllVatAmt,
    LAST_AMT + BFMM_PRE_AMT_ACUM + LAST_VAT + BFMM_PRE_AMT_ACUM_VAT as totalLastAmt,
 SAID_AMT + SAID_PREAMT + SAID_VAT + SAID_PREVAT as totalSaidAmt,
 LAST_AMT + BFMM_PRE_AMT_ACUM + SAID_AMT+SAID_PREAMT + BFMM_PRE_AMT_ACUM_VAT + SAID_VAT + SAID_PREVAT as totalAmt,
    TAX_YN
FROM
    E23_AS_FUND
WHERE
    YYYYY='2010') a,
(select rownum from dict where rownum <=6) b