MSSQL windowing function

Disclaimer: do not like sentimental relentless pain, but also into Qianwan Lv inch. When the angle finite horizon, only Acacia endless place. https://blog.csdn.net/u011078141/article/details/91268197

As a developer is concerned, I feel Baihun in the workplace for many years, he really could be no spirit of it, read the " programmer's SQL Golden " this e-book, so I really do not learn little knowledge of, really like this e-book, to explain the contents of the book is better to understand, but also more practical. The authors thank the hard work :).

Today will be introduced SQL Sever windowing function, what windowing function, do not understand it. Anyway, for me, I was puzzled, for the first time heard. So, what is the windowing function, in fact, can be understood as an enhanced version of the aggregate function. Since then aggregate functions (excluding the case subquery), the entire query can only be polymerized return value columns, but not have a return value of the underlying row. So the need for the return value of the base line, then you need to use complex sub-queries or stored procedures before they can be resolved. But using windowing function can be easily resolved, it can return the column base line at the same time in the same row and column aggregation. In the ISO SQL provides functions such as windowing function, in Oracle is called analytic functions, while in DB2 OLAP function is called.

Windowing function polymerization function, are set on the group of rows is calculated polymerization. It is used as a row definition window (the window where the operator is the set of rows to be operated), it operates a set of values, without using the GROUP BY clause to group the data can be returned in the same row simultaneously rows and columns based polymerization column. Anyway, I understand this function has been used well subquery or otherwise obtain the value of the aggregate columns to me merge.

With examples in the book step by step description, suppose you want to calculate the total number of all personnel, we can execute the following SQL statement:

SELECT COUNT(FName) FROM T_Person

In this manner a direct comparison, only the return value of a polymerization column, there is no value in the column of row basis. But sometimes we need never line aggregate functions access the values ​​in the column (i.e. the column base line) calculated in the polymerization. For example, we would like to check information about each employee wages less than 5,000 yuan (city and age), and show the number of employees all wages less than 5,000 yuan, try to write the following SQL statement in each row:

SELECT FName, FCITY, FAGE, FSalary, COUNT(FName)
FROM T_Person
WHERE FSALARY<5000

After executing the above SQL we get the following error :
selecting columns in the list 'T_Person.FCity' invalid because it is not contained in an aggregate function or the GROUP BY clause.
This is because all columns are not included in an aggregate function must be declared in the GROUP BY clause , a way to use sub-queries can be solved:

Copy the code
SELECT FName, FCITY, FAGE, FSalary,
(
SELECT COUNT(FName) FROM T_Person
WHERE FSALARY<5000
)
FROM T_Person
WHERE FSALARY<5000
Copy the code

Although the use of sub-queries can solve this problem, but the use of sub-query is very troublesome, the use of windowing function you can greatly simplify the implementation, the following SQL statement to show the same effect if you use windowing functions to achieve:

SELECT FName, FCITY, FAGE, FSalary, COUNT(FName) OVER()
FROM T_Person
WHERE FSALARY<5000

Different functions can be seen that polymerization is windowing function after polymerization adds a function OVER keyword.
Windowing function call format is: function name (column) OVER (option)

I used here is the SQL Server 2008 R2, do not know when to begin, SQL SERVER also supports windowing function used in the ORDER BY clause ( Note: books say MSSQLServer is not supported by the windowing function in ORDER BY clause ). Anyway, here I integrate relevant content online. It is because of the windowing function supports the ORDER BY clause after windowing functions are divided into two categories.

The first category: the polymerization windowing function ==== " aggregate function (column) the OVER (option), where options may be a PARTITION BY clause, the ORDER BY clause is not

The second category: Sort windowing function ==== " Sort function (column) OVER (option), this option can be ORDER BY clause can be OVER (PARTITION BY clause ORDER BY clause), but PARTITION BY clause is not

 

Polymerization windowing function

 

OVER keyword indicates that the aggregate function as a polymerization windowing function instead of the aggregate function. Allows all standard SQL aggregation functions polymerization windowing function used.
In the above example, the windowing function COUNT (*) OVER () for each row in the query results to return the number of all eligible rows. OVER parentheses after the keyword option window range often are added for changing the polymerization operation. If the keyword in brackets after the OVER option is empty, the windowing functions for all rows in the result set of the polymerization operation.

 

PARTITION BY clause

 

After OVER windowing function key parentheses may be used to define a partition line PARTITION BY clause to calculate for polymerization. With different GROUP BY clause, PARTITION BY clause partitions created are independent of the result set, the partitions created only for calculation polymerization, and different partitions created windowing function does not affect each other. The following SQL statement shows the number of personnel information as well as the city belongs to each person:

SELECT FName, FCITY, FAGE, FSalary,
COUNT(FName) OVER(PARTITION BY FCITY)
FROM T_Person

The OVER (the PARTITION BY FCITY) represents the result set partitioning according FCITY, and calculates the current row in the polymerization belongs to group results. In the same SELECT statement can be used simultaneously multiple windowing function, the windowing function and do not interfere with each other. For example, the following SQL statement information for the number of each person, the number of those skilled in the city and peers show:

SELECT FName,FCITY, FAGE, FSalary,
COUNT(FName) OVER(PARTITION BY FCITY),
COUNT(FName) OVER(PARTITION BY FAGE)
FROM T_Person

 

Sort windowing function

 

For ordering windowing function is concerned, it supports windowing function are: ROW_NUMBER (line number), RANK (rank), DENSE_RANK (dense rank) and NTILE (grouping ranking).

Look for a SQL statement:

Copy the code
select  FName, FSalary, FCity, FAge,  
row_number() over(order by FSalary) as rownum,  
rank() over(order by FSalary) as rank,  
dense_rank() over(order by FSalary) as dense_rank,  
ntile(6) over(order by FSalary)as ntile 
from  T_Person 
order by  FName  
Copy the code

 Implementation of the results are as follows (for those who want to try their own friends, then you have hard points, download e-books or buy books to learn it because I'm limited space, to omit most of oh.):

 

See the results above it, to introduce the following relevant contents. The end result we get is displayed in ascending order according to FName.

For ROW_NUMBER () over ( Order by FSalary) AS rownum, this sort is based FSalary windowing function to sort in ascending order, and sort the results obtained in Reference

For the Rank () over ( the Order by FSalary) AS Rank, this function is the sort described by FSalary way to sort in ascending order, and sort the results obtained ranking number. This function evaluation results can be arranged out of the rankings, tied for rank after rank will be tied together with the number of bins (simply say that everyone is only one ranking, then two situations tied for first place, this time came in two a behind the first place will be the third, that is, there is no second place, but there are two first place)

For DENSE_RANK () over ( Order by FSalary) AS DENSE_RANK, this function is sorted in ascending order by FSalary be ordered, and the ranking number of the sort result obtained. This function with the rand () function except that, after ranking tied for only tied for plus 1 (simply say that everyone is only one ranking, then two situations tied for first place, this time came in the first two the second will be back, that is, two first, a second)

For NTILE ( . 6 ) over ( Order by FSalary) AS NTILE, this is the sort function by FSalary to sort in ascending order, and the number of packets obtained result of the sort.

Similar sorting and Aggregate windowing function, also supports the statement in the PARTITION BY clause OVER. E.g:

Copy the code
select  FName, FSalary, FCity, FAge,  
row_number() over(partition by FName  order by FSalary) as rownum,  
rank() over(partition by FName order by FSalary) as rank,  
dense_rank() over(partition by FName order by FSalary) as dense_rank,  
ntile(6) over(partition by FName order by FSalary)as ntile 
from  T_Person 
order by  FName
Copy the code

 About PARTITION BY clause, see the above description, there is no longer a burden. But the point to note is that using PARTITION BY clause needs to be placed before the ORDER BY clause in the sort windowing function.

So far this paper finish. 

Guess you like

Origin blog.csdn.net/u011078141/article/details/91268197