Window function @sql learning

 

[Reference link] https://zhuanlan.zhihu.com/p/98655285

 

mysql8.0+ window function

Window function is also called OLAP function (Online Analytical Processing)

1. The grammatical structure of the window function:

 #Key word :Partiton by & order by

< window function > over ([PARTITION by < column list >]    

                     Order by < list of columns for sorting > )  

There are two types of windowing functions:

a. Aggregation functions that can be used as windowing functions: (sum, avg, count, max, min)

b. Special window function: (Rank, Dense_Rank, Row_Number)

 

【1】Realize group ranking effect

[2] Two questions, one is to select the customer with the highest score, and the other is to select the highest score of each user

 

2. Use of dedicated window functions

a) How to use

The Rank function is a function that records the sort order. partition by is used to group the result set. If it is not specified, it treats the entire result set as a group. The difference between it and the aggregation function and groupby is that it can return multiple items in a group records, and aggregate functions generally have only one record reflecting statistical values.

-- 查看表
SELECT * FROM product;

-- Arrange the 8 kinds of commodities in the table according to the product_type and the sale_price according to the serial number

# In the statement, PARTITION By specifies the range of objects to be sorted (horizontally grouping tables),

# order by specifies which column and order to arrange (vertically define the sorting rules).

-- 指定partition by 的字段
SELECT product_id,product_name,product_type,sale_price,
rank() over (PARTITION BY product_type ORDER BY sale_price ASC ) as '排序'
FROM product

-- How would it work without PARTITION By?

# Set the PARTITION By function group as a silent item

select product_id,product_name,product_type,sale_price,
rank() over (order by sale_price asc)  as '排序'
from product

The results are as follows: 1) The data display is not grouped, and it is directly sorted by sale_price

2) When the prices are tied, the sorting results are consistent, and the sorting count value is +1;

 

b) Function difference

There are three kinds of dedicated windowing functions mentioned above: Rank, Dense_Rank, Row_Number, what is the difference between these three?

#The following sorting results of the three functions are defined as sorting 1, sorting 2, and sorting 3, and the results are output:

select product_id,product_name,product_type,sale_price,
rank() over  (order by sale_price asc)  as '排序1',
dense_rank() over  (order by sale_price asc)  as '排序2',
row_number() over  (order by sale_price asc)  as '排序3'
from product

 

Comparing sorting 1 with sorting 2, it can be seen that when the dense_rank function calculates the juxtaposition result, the juxtaposition value does not occupy a place, and when there are two consecutive No. 3 positions, the subsequent counting result is still 4;

Comparing sorting 2 and sorting 3, it can be seen that row-number will calculate a unique value regardless of whether the sale_price value is consistent or not, and will not calculate continuous values.

 

c) aggregate function as window function

Aggregation functions such as (sum, avg, count, max, min) can be referenced to the windowing function:

Use of #sum aggregate functions

select product_id,product_name,product_type,sale_price,
sum(sale_price) over  (order by product_id)  as '累加'
from product

It can be seen that when the above results are accumulated, the sale_price is accumulated, and the cumulative sum of the above results is calculated in sorting 1. The data in the table is arranged in order by order by product_id; similarly, the AVG function will calculate the average value of the accumulated value.

Tips: The aggregation function is used as a window function, which is not as simple as sorting, it may be accumulated, it may be accumulated and averaged

d) Guarantee the order of the sorted results

When sorting according to the above results, sometimes the sorting results are unordered, and you can use double order by to sort them in order:

# Arrange the calculation results again

select product_id,product_name,product_type,sale_price,
rank() over  (order by sale_price )  as '排序1'
from product
order by 排序1 desc

 

-- 按 product_type 统计总金额,及其各产品金额占比
-- 按 product_type 统计金额由小到大进行累计,
SELECT product_id, product_name, product_type, sale_price
	, sum(sale_price) over(PARTITION BY product_type) as typeSum
	, sale_price / sum(sale_price) over(PARTITION BY product_type) as ratioInType
	, sum(sale_price) over(PARTITION BY product_type ORDER BY sale_price) as orderCumsum
FROM product ORDER BY product_type, sale_price;

Guess you like

Origin blog.csdn.net/Cameback_Tang/article/details/108247673