group by 和 over(partition by)的区别

废话不多说,直接来干货!

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 和 over(partition by)的区别_第1张图片

此处小结: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

group by 和 over(partition by)的区别_第2张图片

此处小结:加上order by 后,类似于累加功能(sum_sala += EmpSalary),先观察销售部的结果,从第4条记录开始,其sum(EmpSalary)即sum_sala=2000,第5条记录,sum(EmpSalary)=sum_sala+2500=4500,即第4条sum_sala与第5条EmpSalary的和,依次类推;开发部,由于2个5000是并列的,所以计算的时候是几个并列数据之和即5000+5000=10000。本人小白,如有错误,还望指出,谢谢!!!

你可能感兴趣的:(sql)