Basic usage of SQL window function and aggregate function

window function

When we need to perform some more complex subqueries, the aggregation function will be very troublesome, so we can use the window function to group and then use the function query. The window function can display both the data before aggregation and the data after aggregation , and can return the column value of the basic row and the result column after aggregation in the same row

Common application scenarios: sorting the grades of students in the class
Common window functions
insert image description here

The basic form of the window function

func_name(<parameter>) 
OVER(
[PARTITION BY <part_by_condition>] 
[ORDER BY <order_by_list> ASC|DESC]
[rows between ?? And ??]
)

field explanation

  • func_name: The function that needs to be calculated, here you can fill in the various functions you need
  • over(): operate on the data
    • partition by: Group data according to a field
    • order by: The data in each window is sorted in ascending or descending order according to a certain field
      • eg over(partition by xxx order by yyy): group by xxx and sort by column yyy
    • rows between ... and ...: Subdivision window, select a subset of the current partition, usually used as a sliding window
      • current row: current line
      • unbounded preceding: the first row in the partition
      • unbounded following: the last row in the partition
      • expr preceding: (current row - value of expr) row
      • expr following: row of (current row + value of expr)

Note:
The window clause is missing after the sorting clause, and the window specification defaults to rows between unbounded preceding and current row. Both the
sorting clause and the window clause are missing, and the window specification defaults to rows between unbounded preceding and unbounded following


Partition by

Sales table
insert image description here

select *,
max(sales) over(partition by idate) as '每个月一个员工最大的销量',
max(sales) over(partition by iname) as '每个员工在这几个月里最大的销量'
from Sales;

insert image description here

Order by

Sales table
insert image description here

select *,
row_number() over(order by sales desc) as '按销量进行排序',
row_number() over(partition by idate order by sales desc) as '每个月按销量进行排序'
from Sales;

insert image description here

Rows between and

weather table
insert image description here

select *, 
avg(temperature) over(order by recordDate rows between current row and 1 following) as '今天和明天天气的均值',
avg(temperature) over(order by recordDate rows between 1 preceding and current row) as '昨天和今天天气的均值',
avg(temperature) over(order by recordDate rows between 2 preceding and 1 preceding) as '前天和昨天天气的均值',
avg(temperature) over(order by recordDate rows between current row and unbounded following) as '今天到表里未来天气的均值',
avg(temperature) over(order by recordDate rows between unbounded preceding and current row) as '第一条记录到今天的天气均值'
from weather;

insert image description here

aggregate function

Sales table
insert image description here

-- 这里举例了几个常见的聚合函数
select *, 
max(sales) over(partition by month(idate)) as max,
min(sales) over(partition by month(idate)) as min, 
avg(sales) over(partition by month(idate)) as avg,
sum(sales) over(partition by month(idate)) as sum 
from Sales;

insert image description here



The two examples mainly used here are as follows:

-- Sales表
CREATE TABLE Sales
( 
idate date, 
iname char(2), 
sales int
); 
-- 向表中插入数据 
INSERT INTO Sales VALUES 
('2021/1/1', '丁一', 200), 
('2021/2/1', '丁一', 180), 
('2021/2/1', '李四', 100), 
('2021/3/1', '李四', 150), 
('2021/2/1', '刘猛', 180), 
('2021/3/1', '刘猛', 150), 
('2021/1/1', '王二', 200), 
('2021/2/1', '王二', 180), 
('2021/3/1', '王二', 300), 
('2021/1/1', '张三', 300), 
('2021/2/1', '张三', 280), 
('2021/3/1', '张三', 280); 

-- weather表
drop table if exists weather;
create table weather(
    id int,
    recordDate date,
    temperature int
);
insert into weather
values (1,'2015-01-01',10),
       (2,'2015-01-02',25),
       (3,'2015-01-03',20),
       (4,'2015-01-04',30);

Reference source
MySQL window function
MySQL module: window function
MySQL basics (6) - MySQL window function

Guess you like

Origin blog.csdn.net/weixin_46599926/article/details/128268875