累计数据的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 指定计算当前行和它后面的一行;


猜你喜欢

转载自zhzhiqun2005.iteye.com/blog/2019464