当我在我的事实表上自我加入时,我得到了重复。我想要做的是两次加入事实表以产生一些总计。仅当我加入事实表以获取百分比和生产聚合时,我的 sql 结果才返回重复项目。可能有更好的方法来做到这一点。我愿意提出建议。(DB2)
SELECT distinct
DP.PROJECT_ID,
DP.PROJECT_NAME,
DM.BUILDING_NAME,
DA.AMOUNT,
cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) as GROSS,
(DA.AMOUNT - sum(FAT.TRANSACTION_AMOUNT)) as NET,
PERCENT.PERCENTAGE_USED,
PROD.PRODUCTION
FROM FACT_TABLE as FAT
RIGHT JOIN ALCF_WAREHOUSE.DIM_A DA ON FAT.DIM_A_ID = DA.DIM_A_ID
INNER JOIN ALCF_WAREHOUSE.DIM_PROJECT DP ON FAT.DIM_PROJECT_ID = DP.DIM_PROJECT_ID
INNER JOIN ALCF_WAREHOUSE.DIM_DATE DD ON FAT.START_DATE_DIM_ID = DD.DATE_KEY
INNER JOIN ALCF_WAREHOUSE.DIM_DATE T3 ON FAT.END_DATE_DIM_ID = T3.DATE_KEY
INNER JOIN ALCF_WAREHOUSE.DIM_JOB DJ ON FAT.DIM_JOB_ID = DJ.DIM_JOB_ID
INNER JOIN ALCF_WAREHOUSE.DIM_BUILDING DM ON FAT.BUILDING_ID = DM.BUILDING_ID
INNER JOIN ALCF_WAREHOUSE.DIM_DATE T5 ON DJ.JOB_END_ID = T5.DATE_KEY
INNER JOIN ALCF_WAREHOUSE.DIM_DATE T10 ON DJ.JOB_START_ID = T10.DATE_KEY
INNER JOIN ALCF_WAREHOUSE.DIM_DATE DDS on DDS.DATE_KEY = DA.A_START_DATE_ID
INNER JOIN ALCF_WAREHOUSE.DIM_DATE DDE on DDE.DATE_KEY = DA.A_END_DATE_ID
JOIN
(
SELECT DP.DIM_PROJECT_ID,
concat(((sum(FAT.TRANSACTION_AMOUNT)/DA.AMOUNT)*100),'%') AS Percentage_used
FROM FACT_TABLE as FAT
INNER JOIN DIM_A DA ON FAT.DIM_A_ID = DA.DIM_A_ID
INNER JOIN DIM_PROJECT DP ON FAT.DIM_PROJECT_ID = DP.DIM_PROJECT_ID
WHERE DA.BUILDING_NAME IN ('MAIN', 'SECONDARY')
AND DA.TYPE_NAME = 'INVISION'
GROUP BY DP.DIM_PROJECT_ID, DA.AMOUNT
HAVING concat(((sum(FAT.TRANSACTION_AMOUNT)/DA.AMOUNT)*100),'%') <> '.00%'
) percent on DP.dim_project_id = percent.dim_project_id
JOIN
(
SELECT DJ.DIM_PROJECT_ID, cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) as PRODUCTION
FROM FACT_TABLE as FAT
RIGHT OUTER JOIN DIM_A DA ON FAT.DIM_A_ID = DA.DIM_A_ID
INNER JOIN DIM_JOB DJ ON FAT.DIM_JOB_ID = DJ.DIM_JOB_ID
WHERE DJ.SIZE in ('33% <= x <= 100%', '16% <= x < 33%')
AND DA.BUILDING_NAME IN ('MAIN', 'SECONDARY')
AND DA.TYPE_NAME = 'INVISION'
GROUP BY DJ.DIM_PROJECT_ID
) PROD on DP.dim_project_id = PROD.dim_project_id
WHERE
((DD.DATE_VALUE >= '2013-01-01'
AND T3.DATE_VALUE <= '2014-01-01')
OR (((DD.DATE_VALUE > '2013-01-01'
AND DD.DATE_VALUE < '2014-01-01')
OR (T3.DATE_VALUE <= '2014-01-01'
AND T3.DATE_VALUE > '2013-01-01')
OR (DD.DATE_VALUE <= '2013-01-01'
AND T3.DATE_VALUE > '2014-01-01'))
AND (T5.DATE_VALUE >= '2013-01-01'
AND T5.DATE_VALUE < '2014-01-01')))
AND DM.BUILDING_NAME IN ('MAIN', 'SECONDARY')
AND DA.TYPE_NAME = 'INVISION'
AND DDS.YEAR4 = '2013'
GROUP BY
DP.DIM_PROJECT_ID,
DP.PROJECT_NAME,
DM.MACHINE_NAME,
DA.AMOUNT,
PERCENT.PERCENTAGE_USED,
PROD.PRODUCTION
ORDER BY PROJECT_NAME
上述查询结果:
期望的结果: