我正在尝试计算每种任务类型(绿色/琥珀色/红色)的百分比,并将这些值作为百分比输出到另一列中。但是,这些列的值仅输出为0或1,而不是真实百分比。我无法弄清楚为什么会这样,因为其他列正在输出正确的值。我的逻辑在哪里不正确?
SELECT ValCat.ArgoBU as BusinessUnit, Validations.KpiMetrics, validations.MonthClose,
sum(1) as totTasks,
sum(iif(colour='Green',1,0)) as isGreen,
sum(iif(colour='Amber',1,0)) as isAmber,
sum(iif(colour='Red',1,0)) as isRed,
--calculate percent of green/amber/red and display in separate column
--this is where its outputting 0/1, not desired percentage
format((sum(iif(colour='Green',1,0))/sum(1)), 'P') as PercGreen,
format((sum(iif(colour='Amber',1,0))/sum(1)), 'P') as PercAmber,
format((sum(iif(colour='Red',1,0))/sum(1)), 'P') as PercRed
FROM validations
INNER JOIN valcat on validations.BusinessUnit = ValCat.BusinessUnit
GROUP BY ArgoBU, KpiMetrics, MonthClose
例如,第1行显示totTasks:1651,isGreen:983,但是PercGreen显示0而不是59.5%。我不知道为什么会这样,因为这些列的代码是完全一样的。
答案 0 :(得分:0)
尝试使用sum(1.0)或将其转换为百分比计算中的小数。如果两个都是整数,结果将是整数,因此您将看到0/1。
例如:
SELECT format((sum(iif(color='GREEN',1,0))/sum(1.0)), 'P') PercGreen,--updated
, (sum(iif(color='GREEN',1,0))/sum(1)) GreenDivInt --original
FROM
(
SELECT 'GREEN' AS COLOR
UNION ALL
SELECT 'GREEN' AS COLOR
UNION ALL
SELECT 'GREEN' AS COLOR
UNION ALL
SELECT 'GREEN' AS COLOR
UNION ALL
SELECT 'GREEN' AS COLOR
UNION ALL
SELECT 'GREEN' AS COLOR
UNION ALL
SELECT 'GREEN1' AS COLOR
UNION ALL
SELECT 'GREEN1' AS COLOR
UNION ALL
SELECT 'GREEN1' AS COLOR
UNION ALL
SELECT 'GREEN1' AS COLOR
) S