Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Sum Rows based on a condition

$
0
0
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. The attached thumbnails display the current results and desired output.

Code:

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.MACHINE_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


Attached Images
File Type: png Capture1.PNG (20.9 KB)
File Type: png Capture.PNG (11.7 KB)

Viewing all articles
Browse latest Browse all 13329

Trending Articles