The difference between group by and over (partition by)

1>Create a table and insert data, because it overis a windowing function, it mysqldoes not support windowing functions, oracle,sql server,db2...and other new versions such as those are supported (here we borrow data from others)

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>Calculate the total salary of each department?

2.1>group by

SELECT EmpDepartment,SUM(EmpSalary) sum_sala FROM Employee GROUP BY EmpDepartment

Insert picture description here
2.2>over(partition by)

SELECT EmpSalary,EmpDepartment,SUM(EmpSalary) OVER(PARTITION BY EmpDepartment) sum_sala FROM Employee

Insert picture description here

Here Summary: group byand partition byare grouped statistical functions, but partition bydoes not have group bya summary function. partition byEvery record of statistics exists, and group byall records are summarized into one record (similar distinct EmpDepartmentto deduplication). partition byIt can be used in conjunction with aggregate functions and has other advanced functions.

3>Add partition byafterorder by

SELECT EmpSalary,EmpDepartment,SUM(EmpSalary) OVER(PARTITION BY EmpDepartment ORDER BY EmpSalary) sum_sala FROM Employee

Insert picture description here

Here is a summary: After adding order by, similar to the accumulation function ( sum_sala += EmpSalary), first observe the results of the sales department, starting from the fourth record, which sum(EmpSalary)is sum_sala=2000the fifth record, sum(EmpSalary)=sum_sala+2500=4500which is the sum of the fourth sum_salaand the fifth EmpSalary, in turn Analogy; in the development department, since two 5000s are parallel, the sum of several parallel data is 5000+5000=10000 when calculating.


Windowing function over ( partition by)well and group bydifferentiated

1. How overto write the function:

overpartition by class order by sroce)

sroceAccumulate according to sorting, it order byis a default windowing function, according to classpartition.

2. Window scope of opening window:

 overorder by sroce range between 5 preceding and 5 following

The window range is within the range of the current row data amplitude minus 5 plus 5.

 overorder by sroce rows between 5 preceding and 5 following

The window range is 5 rows before and after the current row.

 sum() over(partition byorder by):求分组后的总和。 
 first_value() over(partition byorder by):求分组后的第一个。 
 last_value() over(partition byorder by):求分组后的最后一个。 
 count() over(partition byorder by):求分组后的总数。 
 max() over(partition byorder by):求分组后的最大值。 
 min() over(partition byorder by):求分组后的最小值。 
 avg() over(partition byorder by):求分组后的平均值。 
 lag() over(partition byorder by):取出前n行数据。   
 lead() over(partition byorder by):取出后n行数据。

3. over partition bythe group bydifference

Original table:

NAME     DEPT    SALARY
A         10     1000
B         10     2000
C         20     1500
D         20     3000
E         10     1000   

With this, over partition byI can query the original specific information of each employee and the total salary of its department:

select name,dept,salary,sum(salary) over (partition by dept) total_salary from salary;  
name     dept      salary      tatal_salary
A        10        1000        4000
B        10        2000        4000
E        10        1000        4000
C        20        1500        4500
D        20        3000        4500

There is goup byno way to do this with use , only the total salary of each department can be queried:

select dept,sum(salary) total_salary from salary group by dept
dept      total_salary
10        4000
20        4500

over partition byThe details of each data
group bywill be displayed , and the aggregation will display multiple items. The aggregation will only display one item.


over partition byWith group bydistinction

group byOnly grouped statistical data over partition bycan be obtained , not only grouped statistical data can be obtained, but detailed data can also be displayed at the same time.
group byIn the wherefollowing clause; over partition byit is frombefore clause.

over partition byAnd group byare used with functions like statistics, these two have what difference does it make?
At present, I only know one such difference: For
example, there is a table saraly:

CREATE TABLE SALARY AS 
SELECT 'A' NAME,10 DEPT,1000 SALARY FROM DUAL 
UNION ALL 
SELECT 'B',10,2000 FROM DUAL 
UNION ALL 
SELECT 'C' ,20,1500 FROM DUAL 
UNION ALL 
SELECT 'D',20,3000 FROM DUAL 
UNION ALL
SELECT 'E',10,1000 FROM DUAL;
NAME DEPT SALARY
A         10     1000
B         10     2000
C         20     1500
D         20     3000
E         10     1000  

With this, over partition byI can query the original specific information of each employee and the total salary of its department:

select name,dept,salary,sum(salary) over (partition by dept) total_salary from salary; 
name       dept         salary      tatal_salary
A        10        1000        4000
B        10        2000        4000
E        10        1000        4000
C        20        1500        4500
D        20        3000        4500

There is goup byno way to do this with use , only the total salary of each department can be queried:

select dept,sum(salary) total_salary from salary group by dept
dept        total_salary
10        4000
20        4500

In addition, over partition byyou can also query the percentage of each employee in the department's total salary:

select name,dept,salary,salary*100/sum(salary) over (partition by dept) percent from salary;
name       dept         salary     percent
A        10        1000        25
B        10        2000        50
E        10        1000        25
C        20        1500        33.3333333333333
D        20        3000        66.6666666666667

With group byno way to do this. I do not know my understanding is not correct, please friends advice, especially over partition bywith the group bymore differentiated please share, thank you!

Guess you like

Origin blog.csdn.net/WuLex/article/details/115037696