0
我正在使用db2數據庫(不確定版本)。我的腳本是功能性的,但我沒有產生我期望的結果。我需要根據我的情況聲明中的條件將我的總計總計分成3組,但是對於同一個project_name/id和building_name/id下的每個項目,我需要每個項目的每個項目名稱/ id只有一行的總數building_name/ID。我假設某個組或某個遞歸函數是解決方案,但我不太確定。希望能朝着正確的方向前進。這是我的腳本。總結基於條件的行
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
結果:
期望的結果: