Domanda

I'm using a db2 database (not sure of the version). My script is functional but I'm not producing the results I expect. I need to separate my aggregate totals in to 3 groups based on conditions in my case statement but for every project under the same project_name/id and building_name/id, I need the sum total under each bucket having only one row per project_name/id and building_name/id. I assume a group by of some sort or a recursive function would be the solution but I'm not quite sure. Would appreciate a push in the right direction. Here is my script.

    SELECT
        DP.DIM_PROJECT_ID,
        DP.PROJECT_NAME,
        DM.DIM_BUILDING_ID,
        DM.BUILDING_NAME,
        CASE WHEN ( DJ.GROUPS3 IN ('33.3% <= x <= 100%', '16.7% <= x < 33.3%') AND DA.TYPE_NAME = 'SALES')  
        THEN cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) 
        WHEN (DJ.GROUPS3 IN ('60% <= x <= 100%', '20% <= x < 60%') AND DA.TYPE_NAME = 'SALES')
        THEN cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) 
        ELSE '0'    
        END as CAPABILITY,      
        CASE WHEN (DJ.GROUPS3 = '0% <= x < 16.7%' AND DA.TYPE_NAME = 'SALES')
        THEN cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) 
        WHEN (DJ.GROUPS3 = '0% <= x < 20%' AND DA.TYPE_NAME = 'SALES')
        THEN cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) 
        ELSE '0'            
        END as GROUP_1, 
        CASE WHEN (DJ.GROUPS3 = '16.7% <= x < 33.3%' AND DA.TYPE_NAME = 'SALES')
        THEN cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) 
        WHEN (DJ.GROUPS3 = '20% <= x < 60%' AND DA.TYPE_NAME = 'SALES') 
        THEN cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer)     
        ELSE '0'        
        END as GROUP_2,
        CASE WHEN (DJ.GROUPS3 = '33.3% <= x <= 100%' AND DA.TYPE_NAME = 'SALES') 
        THEN cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) 
        WHEN (DJ.GROUPS3 = '60% <= x <= 100%' AND DA.TYPE_NAME = 'SALES') 
        THEN cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer)
        ELSE '0'            
        END as GROUP_3
FROM FACT_TABLE as FAT
RIGHT JOIN DIM_ALLOCATION DA ON FAT.DIM_ALLOCATION_ID = DA.DIM_ALLOCATION_ID
INNER JOIN DIM_PROJECT DP ON FAT.DIM_PROJECT_ID = DP.DIM_PROJECT_ID
INNER JOIN DIM_DATE DD ON FAT.ALLOCATION_START_DATE_DIM_ID = DD.DATE_KEY
INNER JOIN DIM_JOB DJ ON FAT.DIM_JOB_ID = DJ.DIM_JOB_ID
INNER JOIN DIM_BUILDING DM ON FAT.DIM_BUILDING_ID = DM.DIM_BUILDING_ID

WHERE
    DD.DATE_VALUE = '2013'
    AND DA.BUILDING_NAME IN ('ADMIN', 'INVISION')


GROUP BY DM.DIM_BUILDING_ID,
         DP.DIM_PROJECT_ID, 
         DP.PROJECT_NAME,
         CAPABILITY,
         DM.BUILDING_NAME, 
         DJ.GROUPS3,
         DA.TYPE_NAME
ORDER BY DP.PROJECT_NAME

Results: enter image description here

Desired Results: enter image description here

È stato utile?

Soluzione

I found the answer and have posted it below.

cast(SUM(CASE WHEN DJ.GROUPS3 IN ('33.3% <= x <= 100%', '16.7% <= x < 33.3%')
    THEN cast(FAT.TRANSACTION_AMOUNT as real)
    WHEN DJ.GROUPS3 IN ('60% <= x <= 100%', '20% <= x < 60%')
    THEN cast(FAT.TRANSACTION_AMOUNT as real)
    ELSE NULL
    END)) as integer)  as CAPABILITY,

You take the sum and cast outside the case statement.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top