我希望使用db2获得一笔总和。我明显得到-204错误,因为我正在尝试使用别名来计算聚合。我不知道如何处理这个问题并希望得到一些帮助。
SELECT
DA.AMOUNT,
cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) as time_used,
(DA.AMOUNT - sum(FAT.TRANSACTION_AMOUNT)) as time_to_use,
concat(((sum(FAT.TRANSACTION_AMOUNT)/DA.AMOUNT)*100),'%') AS Percentage_used,
**sum(time_used)/SUM(DA.AMOUNT) AS Total_Percentage_Used**
FROM FACT_Table as FAT
RIGHT DIM_AWARD DA ON FAT.DIM_ID = DA.DIM_ID
group by DA.AMOUNT
答案 0 :(得分:1)
一种方法是在表达式中替换金额:
SELECT DA.AMOUNT,
cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer) as time_used,
(DA.AMOUNT - sum(FAT.TRANSACTION_AMOUNT)) as time_to_use,
concat(((sum(FAT.TRANSACTION_AMOUNT)/DA.AMOUNT)*100),'%') AS Percentage_used,
cast(sum(cast(FAT.TRANSACTION_AMOUNT as real)) as integer)/DA.AMOUNT AS Total_Percentage_Used
FROM FACT_Table as FAT
RIGHT DIM_AWARD DA ON FAT.DIM_ID = DA.DIM_ID
group by DA.AMOUNT;
由于DA.AMOUNT
中有group by
,因此您无需将其包含在sum()
中。
如果您担心sum()
为NULL
,请使用coalesce()
:
coalesce(cast(sum(cast(FAT.TRANSACTION_AMOUNT as real))为整数),0.0)/ DA.AMOUNT AS Total_Percentage_Used