sum() over(partition by order by row between ... )

原始数据
在这里插入图片描述

SELECT NAME,
       p_date,
       cost,
       sum(cost) over() AS sample1, --所有行相加
       sum(cost) over(PARTITION BY NAME) AS sample2,--按name分组,组内所有行相加
       sum(cost) over(PARTITION BY NAME ORDER BY p_date) AS sample3,--按name分组,组内按日期累加(有相同日期也累加)
       sum(cost) over(PARTITION BY NAME ORDER BY p_date,cost) AS sample8,--按name分组,组内按顺序累加
       sum(cost) over(PARTITION BY NAME ORDER BY p_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS sample4,--和sample3一样,由起点到当前行的聚合
       sum(cost) over(PARTITION BY NAME ORDER BY p_date ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS sample5,--前面一行和当前行做聚合
       sum(cost) over(PARTITION BY NAME ORDER BY p_date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS sample6,--前面一行和后面一行做聚合
       sum(cost) over(PARTITION BY NAME ORDER BY p_date ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS sample7 --当前行和后面所有行
FROM tmp_export.test123


注意下红框的数据,相同日期
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值