我正在使用 sql 和 tableau 创建燃尽图。我编写了一个 SQL 查询来为我完成大部分计算。我有一个看起来像这样的表结构
ld.cal_dt | ld.camp_name | ld.ld_cnt | dy_cmp.cal_dt | dy_cmp.camp_name | dy_comp.com_cnt | brndn |
---|---|---|---|---|---|---|
2023-09-05 | Ex-000010 | 62 | 空 | 空 | 空 | 空 |
2023-09-06 | Ex-000010 | 0 | 2023-09-06 | Ex-000010 | 54 | -54 |
2023-09-08 | Ex-000010 | 0 | 2023-09-08 | Ex-000010 | 1 | -55 |
下面是我查询的相关部分的样子
select lc.calendardate,
lc.campaign_name,
lc.loaded_count,
dcc.calendardate,
dcc.campaign_name,
dcc.completed_call_count,
sum(cast(lc.loaded_count as int) - cast(dcc.completed_call_count as int)) over(partition by lc.campaign_name order by lc.calendardate asc) as burn_down
from adjusted_loaded_count as lc
left join adjusted_daily_calls_completed as dcc on
lc.calendardate = dcc.calendardate and
lc.campaign_name = dcc.campaign_name
where lc.campaign_name is not null
我在燃尽专栏中得到了意想不到的结果。我预计结果是 8,然后是 7,而不是 -54 和 -55。为什么我实际上没有得到两个数字之间的差异?
三行:
ld_cnt | com_cnt | 差异 | 到目前为止的总和 |
---|---|---|---|
62 | 空 | 空 | 空 |
0 | 54 | -54 | -54 |
0 | 1 | -1 | -55 |
你想要
ld_cnt | com_cnt | 差异 | 到目前为止的总和 |
---|---|---|---|
62 | 将 NULL 视为 0 | 0 | 62 |
0 | 54 | -54 | 8 |
0 | 1 | -1 | 7 |
您希望将 NULL 视为零。为此使用
COALESCE
:
SUM(
COALESCE(CAST(lc.loaded_count as int), 0) -
COALESCE(CAST(dcc.completed_call_count as int), 0)
) OVER (PARTITION BY lc.campaign_name
ORDER BY lc.calendardate ASC) AS burn_down