累计数据的SQL写法

在常常会用到累计数据的分析,例如:历史累计量,按年累计量,按月计算累计量,按周计算累计量。
下面是一个通过分析函数来实现的累计数据分析:
历史累计: 是最容易的,直接用一个分析函数:
语法:sum(XX) over (partition by country order by date) 这个函数的意思是指: 对XX这个指标在country的分组,并且在date的顺序进行做累计数据计算
例如:

select t.date_id, t.country_id, t.platform_id,t.newuser, sum(t.newuser) over(partition by t.country_id, t.platform_id order by t.date_id) as totaluser_history from BROWSER_USE_OS_F t where t.date_id>=20130101 and t.country_id=2 and t.platform_id=4

row date_id country_id platform newuser totaluser_history
1 20130101 2 4 13262 13262
2 20130102 2 4 15553 28815
3 20130103 2 4 16418 45233
4 20130104 2 4 16524 61757
5 20130105 2 4 17093 78850
6 20130106 2 4 16316 95166
7 20130107 2 4 15965 111131
8 20130108 2 4 16496 127627
9 20130109 2 4 17185 144812
10 20130110 2 4 16770 161582
按年累计: 这种可以演化推广到周累计,月累计,季累计等等。
语法:
sum(t.newuser) over(partition by t.country_id, t.platform_id order by t.date_id  rows between to_date(date_id,'yyyy-mm-dd')-to_date(substr(date_id,0,4)||'0101','yyyy-mm-dd') preceding  and current row) as totaluseryesr


这个语句看起来复杂,其实可以其实也简单,就是在原来的基础上添加了 rows between 1 preceding and current row 这种样式的内容,添加了窗口,并且这个窗口的数据区间是跟据时间来变化的。
to_date(date_id,'yyyy-mm-dd')-to_date(substr(date_id,0,4)||'0101','yyyy-mm-dd')
这一行的意思是,计算当前年到当前数据一共有多少行,通过行数来计算。

row between XX preceding and XX following 样式的类容详细总结如下:

range between unbounded preceding and current row 指定计算当前行开始、当前行之前的所有值;

rows between 1 preceding and current row 指定计算当前行的前一行开始,其范围一直延续到当前行;

range between current row and unbounded following 指定计算从当前行开始,包括它后面的所有行;

rows between current row and 1 following 指定计算当前行和它后面的一行;
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值