1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] ORACLE DECODE with multiple expressions or multiple decodes

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 10, 2024 às 05:12.

  1. Stack

    Stack Membro Participativo

    I have the below data in an Oracle table which I'm trying to perform calculations based on TYPE & QT columns.

    TYPE QT STATUS CNT
    ---- -- ------ ---
    E A YES 123
    E A NO 2540
    E D YES 2
    E D NO 787
    O A YES 884
    O A NO 6957
    O D YES 25
    O D NO 1360


    Looks like my expected output now is different.. like below :

    TYPE TOTAL A A&Y
    ---- ----- ---- -----
    E+O 12678 10504 1007
    E 3452 2663 123


    My old oracle sql is below but the output is wrong when calculating the percentages:

    SELECT * FROM (
    SELECT NVL(SUM(DECODE(A.QT,'A',A.CNT,'D',A.CNT)),0) AS "TOTAL",
    NVL(SUM(DECODE(A.QT,'A',A.CNT)),0) AS "A ALL",
    NVL(SUM(DECODE(A.QT,'A',ROUND(100*(NVL(A.CNT,0)/ A.CNT),2))),0) "A ALL %",
    NVL(SUM(DECODE(A.STATUS,'E',A.CNT)),0) AS "E TOTAL",
    NVL(SUM(DECODE(A.STATUS,'E',DECODE(A.QT,'A',A.CNT))),0) AS "E & A TOTAL"",
    NVL(SUM(DECODE(A.STATUS,'E',ROUND(100 * SUM((DECODE(A.QT,'A',A.CNT)) / SUM(A.CNT)),3))),0) "E & A %"
    FROM ALLIN A)

    Continue reading...

Compartilhe esta Página