开窗函数 sum 应用
问题 : 按日展示,月度累计结果
数据表 CLSFD_WEEKLY_Bn_USV_Watch
select
metric_name,month(clsfd_sum_dt) as mon,
clsfd_sum_dt,
metric_value,
sum(count_name) over(partition by metric_name,month(clsfd_sum_dt) order by month(clsfd_sum_dt),clsfd_sum_dt rows between unbounded preceding and current row) as account
from CLSFD_WEEKLY_Bn_USV_Watch a
order by
month(clsfd_sum_dt),clsfd_sum_dt
运算结果
函数说明
sum(count_name)
的求和是针对后面over()
窗口的求和,
over
中partition by metric_name,month(clsfd_sum_dt)
针对metric_name
这一组按照月份排序,rows between unbounded preceding and current row
限定了行是按照在当前行不限定的往前处理,通俗就是处理当前以及之前的所有行的sum
,即3月5号时sum(count_name)
求的是3月6号之前的和。unbounded
意思无限的 preceding
在之前的,current row
当前行。