1>Create a table and insert data, because it over
is a windowing function, it mysql
does 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
2.2>over(partition by)
SELECT EmpSalary,EmpDepartment,SUM(EmpSalary) OVER(PARTITION BY EmpDepartment) sum_sala FROM Employee
Here Summary: group by
and partition by
are grouped statistical functions, but partition by
does not have group by
a summary function. partition by
Every record of statistics exists, and group by
all records are summarized into one record (similar distinct EmpDepartment
to deduplication). partition by
It can be used in conjunction with aggregate functions and has other advanced functions.
3>Add partition by
afterorder by
SELECT EmpSalary,EmpDepartment,SUM(EmpSalary) OVER(PARTITION BY EmpDepartment ORDER BY EmpSalary) sum_sala FROM Employee
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=2000
the fifth record, sum(EmpSalary)=sum_sala+2500=4500
which is the sum of the fourth sum_sala
and 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 by
differentiated
1. How over
to write the function:
over(partition by class order by sroce)
sroce
Accumulate according to sorting, it order by
is a default windowing function, according to class
partition.
2. Window scope of opening window:
over(order 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.
over(order by sroce rows between 5 preceding and 5 following)
The window range is 5 rows before and after the current row.
sum() over(partition by … order by …):求分组后的总和。
first_value() over(partition by … order by …):求分组后的第一个。
last_value() over(partition by … order by …):求分组后的最后一个。
count() over(partition by … order by …):求分组后的总数。
max() over(partition by … order by …):求分组后的最大值。
min() over(partition by … order by …):求分组后的最小值。
avg() over(partition by … order by …):求分组后的平均值。
lag() over(partition by … order by …):取出前n行数据。
lead() over(partition by … order by …):取出后n行数据。
3. over partition by
the group by
difference
Original table:
NAME DEPT SALARY
A 10 1000
B 10 2000
C 20 1500
D 20 3000
E 10 1000
With this, over partition by
I 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 by
no 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 by
The details of each data
group by
will be displayed , and the aggregation will display multiple items. The aggregation will only display one item.
over partition by
With group by
distinction
group by
Only grouped statistical data over partition by
can be obtained , not only grouped statistical data can be obtained, but detailed data can also be displayed at the same time.
group by
In the where
following clause; over partition by
it is from
before clause.
over partition by
And group by
are 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 by
I 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 by
no 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 by
you 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 by
no way to do this. I do not know my understanding is not correct, please friends advice, especially over partition by
with the group by
more differentiated please share, thank you!