[SQL Excavator] - Introduction to Window Functions

introduce:

Window functions are also known as OLAP functions. OLAP is the abbreviation of OnLine Analytical Processing, which means real-time analysis and processing of database data. A window function is a powerful SQL function for performing aggregate calculations and sorting operations. They can create a window (window) in the query result set and perform calculations on the window without affecting the result set as a whole.

Window functions are often used with an over clause to define the extent of the window. The over clause can specify the sorting method, partitioning method and boundary of the window, etc.

For ease of understanding, it is called a window function. Conventional select statements query the entire table, while window functions allow us to select a certain part of the data for aggregation, calculation and sorting.

usage:

The general form of a window function:

<窗口函数> over ([ partition by <列名> ] [ order by <排序用列名> ])  

The content in [ ] can be omitted.

The key to the window function is to understand the role of the keywords partiton by and order by.

  • The optional parameter of the partiton by clause indicates how to divide the query rows into groups, which is similar to the grouping function of the group by clause, but the partition by clause does not have the summary function of the group by clause and will not change the original table The number of rows logged.
  • The order by clause is an optional parameter, indicating how to sort the rows in each partition, that is, to determine which rules (fields) are sorted in the window.

Notice:

Although both the partiton by clause and the order by clause are optional parameters, the two parameters cannot be absent at the same time (choose one at least). Otherwise, the use of <window function> over( ) is useless (the window consists of all query rows, and the window function uses all rows to compute the result).

Classification:

Commonly used window functions are:

  • row_number(): Returns a unique value for each row, usually used to number the rows in the result set.
  • rank() and dense_rank(): Assigns a rank to each row in the result set according to the specified sort order. rank() skips the same rank when it encounters the same value, while dense_rank() does not.
  • lag() and lead(): The lag function is used to obtain the value of a row before the current row, and the lead function is used to obtain the value of a row after the current row. They are related to sorting and can be used to find the value of the previous or next row.
  • Aggregate functions such as sum(), avg(), min(), max(): These aggregate functions can perform calculations over a window range and return the aggregated value for each row in the result set.

In addition to the functions listed above, there are other types and variants of window functions, which can be selected and used according to specific needs.

Window functions are widely used in SQL, and can be used to calculate moving averages, cumulative sums, sorting within groups, etc. They provide a flexible and efficient way to handle complex query requirements.

Guess you like

Origin blog.csdn.net/qq_40249337/article/details/131988970