我想创建数据透视表 2 的指标:实际/目标。但有些 Target 为空。我在自定义 SQL 中编写如下,但它不起作用。我改成了!= Null或者!=0,但是还是不行。请帮忙!
Case
when TARGET is NULL then 0
else SUM(Revenue)*100/Sum(TARGET)
END AS [%Actual/Target]
感谢和问候
我改成了!= Null或者!=0,但是还是不行。
不要测试 NULL,而是翻转测试来测试
sum(target)
是否大于零(因此避免同时除以零和 NULL):例如
select
case when Sum(TARGET) > 0 then SUM(Revenue)*100/Sum(TARGET) else 0 end as pct
, SUM(Revenue) as sum_revenue
, Sum(TARGET) as sum_target
from t
另一种方法是“嵌套”查询并将百分比计算移至最外层(顶部)查询,如下所示:
select
case when sum_target <> 0 then sum_revenue / sum_target else 0 end as pct
, sum_revenue
, sum_target
from (
select
SUM(Revenue) as sum_revenue
, Sum(TARGET) as sum_target
from t
)