废话不多说,直接来干货!
1>建表并插入数据,因为over是开窗函数,mysql不支持开窗函数,其他如oracle,sql server,db2...等新版本都支持(这里借用他人的数据)
CREATE TABLE Employee
(
ID number(10) not null primary key,
EmpName varchar(20),
EmpSalary varchar(10),
EmpDepartment varchar(20)
);
insert all into Employee values(1,'张三','5000','开发部')
into Employee values(2,'李四','2000','销售部')
into Employee values(3,'王麻子','2500','销售部')
into Employee values(4,'张三表叔','8000','开发部')
into Employee values(5,'李四表叔','5000','开发部')
into Employee values(6,'王麻子表叔','5000','销售部')
select 1 from dual
2>计算每个部门的总薪水?
2.1>group by
SELECT EmpDepartment,SUM(EmpSalary) sum_sala FROM Employee GROUP BY EmpDepartment
2.2>over(partition by)
SELECT EmpSalary,EmpDepartment,SUM(EmpSalary) OVER(PARTITION BY EmpDepartment) sum_sala FROM Employee
此处小结:group by 和 partition by 都有分组统计的功能,但是partition by并不具有group by的汇总功能。partition by统计的每一条记录都存在,而group by将所有的记录汇总成一条记录(类似于distinct EmpDepartment 去重)。partition by可以和聚合函数结合使用,同时具有其他高级功能。
3>在partition by 后在加上order by
SELECT EmpSalary,EmpDepartment,SUM(EmpSalary) OVER(PARTITION BY EmpDepartment ORDER BY EmpSalary) sum_sala FROM Employee