我目前正在使用以下公式计算总计百分比,但公式计算为每个分区总计的移动百分比。有没有办法让公式计算每个分区的总数?
SELECT WINC_MAIN_DECAY.*,
WINC_MAIN_DECAY.acquisition/NULLIF(SUM(acquisition) OVER(PARTITION BY master_show_name, spend_count ORDER BY master_date ASC),0) AS perc_total
FROM WINC_MAIN_DECAY
结果如下:
master_date master_show_name spend spend _count acquisition perc_total
1/4/16 ABC $101,010 1 10 1
1/11/16 ABC 1 10 0.5
1/18/16 ABC 1 5 0.2
1/25/16 ABC 1 2 0.074074074
2/1/16 ABC 1 1 0.035714286
2/8/16 ABC 1 2 0.066666667
2/15/16 ABC 1 1 0.032258065
2/22/16 ABC $101,010 2 6 1
2/29/16 ABC 2 3 0.333333333
3/7/16 ABC $101,010 3 5 1
答案 0 :(得分:1)
如果您不想要累积总和,而是需要每个分区的直接求和,那么您需要做的就是从order by
窗口函数中删除sum
子句。换句话说,而不是:
SUM(acquisition) OVER(PARTITION BY master_show_name, spend_count ORDER BY master_date ASC)
将其更改为:
SUM(acquisition) OVER(PARTITION BY master_show_name, spend_count)