MYSQL 寒假自学 2022 十二 ( 八)

本文深入探讨了SQL中的窗口函数,包括如何使用SUM()函数进行分组内累计求和,通过ORDER BY子句控制计算范围。从无边界到有边界,从当前行到后续行的各种情况逐一解析,展示了窗口函数在数据聚合和分析中的灵活性。
摘要由CSDN通过智能技术生成
use mydb4;
select 
 dname,
 ename,
 hiredate,
 salary,
 sum(salary) over (partition by dname order by hiredate) as pv1
from employee;


select 
 dname,
 ename,
 hiredate,
 salary,
 sum(salary) over (partition by dname ) as pv1
from employee;# 将order by 删去,全部求和

select 
 dname,
 ename,
 hiredate,
 salary,
 sum(salary) over (partition by dname order by hiredate) as pv1
from employee;

-- 控制相加的范围
select 
 dname,
 ename,
 hiredate,
 salary,
 sum(salary) over (partition by dname order by hiredate rows between unbounded preceding and current row) as pv1
from employee; #从分组后的开始行加到当前行

select 
 dname,
 ename,
 hiredate,
 salary,
 sum(salary) over (partition by dname order by hiredate rows between 3 preceding and current row) as pv1
from employee; #从分组后,当前行的前三行一直加到当前行

select 
 dname,
 ename,
 hiredate,
 salary,
 sum(salary) over (partition by dname order by hiredate rows between 3 preceding and 1 following) as pv1
from employee; #从分组后,当前行的前三行一直加到当前行的后一行

select 
 dname,
 ename,
 hiredate,
 salary,
 sum(salary) over (partition by dname order by hiredate rows between current row and  unbounded following) as pv1
from employee; #从当前行一直加到最后一行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值