首先,很明确的知道partition by 是个开窗函数,对于处理累计值我常常会用。
但是,简单粗暴的直接用此函数,会可能丢数据。
比如以下数据:
id type datekey
1 0 20140131
2 1 20140131
3 0 20140228
4 1 20140228
5 0 20140331
6 1 20140331
7 0 20140430
8 1 20140430
9 0 20140531
10 1 20140531
11 0 20140630
12 1 20140630
13 0 20140731
14 1 20140731
15 2 20140731
16 0 20140831
17 1 20140831
算每个月的累计值 直接 count(id) over (partition by substr(datekey,1,4))
计算结果会在8月份丢失type为2的数据的累计
解决办法为
重新建立一个type表
type typename
0 0
1 1
2 2
与上边的表1inner join 即可