- 测试数据
select*fromwmg_test;
- 按照 v2 排序,累计 n+n-1+....+1
selectv1,v2,sum(v2) over(orderbyv2) assumfromwmg_test;
- 先分组,组内在进行上一个案例中的操作
selectv1,v2,sum(v2) over(partitionbyv1orderbyv2) assumfromwmg_test;
- 稳定排序
selectv1,v2,sum(v2) over(partitionbyv1orderbyv1) assumfromwmg_test;
- 相同 key 的进行回填处理
selectv1,v2,sum(v2) over(partitionbyv1) assumfromwmg_test;
- 取一条
selectdistinctv1,sum_01from ( selectv1,sum(v2) over(partitionbyv1) assum_01fromwmg_test) a;
- 当然也可以逆序累加,只需 order by desc 即可
总结(group by & partition by)
- group 单纯分组
- partition 也能分组,但还具备累计的功能