Window functions in SQL Server (2012 new function)

Brief introduction

    SQL Server for post-2012 window function has been greatly enhanced, but for many developers, for window functions are poorly understood, leading to so powerful is wasted, so this article mainly to talk in SQL Server the concept of the window function.

 

What is the window function

    Window function, may also be referred to as OLAP analysis function or functions. Appreciated from the window function may aggregate function begins appreciated, we know the concept of aggregation function, is the value of a column in a plurality of rows as a single line in accordance with the aggregation rules, such as Sum, AVG, etc., simple concept shown in Fig.

1

1. FIG aggregate function

 

    Thus, generally speaking, the number of lines after polymerization should be less than the number of rows prior to polymerization. For the window function, the input is equal to the output result, cite a simple example, if you type A computing products and product type B, A subclass Total 5, B 2 sub-subclasses product, a result of applying the window function after 7 line can still, a window function is applied after Count, attached to each line, for example "a product, a subclass 1,5", and then becomes small class B "B product, subcategories B 1, 2 "the last one is the result of applying the window function.

    Now we have a preliminary overview of the window function, the article I will provide some specific examples of window functions to make the concept more profound, window function in addition to the above-mentioned input line equal to the output line, there are the following features and benefit:

  • Similar polymerization of Group By
  • Non-sequential data access
  • Function may be used for the analysis window function, the ranking function and aggregation functions
  • Simplifies SQL code (elimination Join)
  • Eliminating the middle of the table

    Window function is part of the entire SQL statement is executed last, which means that the window function is performed on the result set of SQL queries, and therefore will not be affected Group By, Having, Where clause.

    A typical example of a window function that we used in the following SQL Server 2005 ranking functions, such as the code shown in Listing 1.

Row_Number() OVER (partition by xx ORDER BY xxx desc) RowNumber

Listing 1. The sort function may be used for pagination

 

    Therefore, we can abstract out of the window function syntax, as shown in Listing 2.

Function () Over (PARTITION By column 1, column 2, Order By column 3, window clause) AS column aliases

Listing 2. Syntax window function

 

A simple example

    Let's look at a simple example, if we want to say AdventureWorks sample database Employee table by sex polymerization, for example, I hope the result is: "login name, gender, gender total number of all employees," if we use traditional wording, it must be related to the sub-queries, such as the code shown in Listing 3.

SELECT [LoginID],gender,
(SELECT COUNT(*) FROM [AdventureWorks2012].[HumanResources].[Employee] a WHERE a.Gender=b.Gender) AS GenderTotal
  FROM [AdventureWorks2012].[HumanResources].[Employee] b

Listing 3. The traditional wording

 

    If we use the window function, the code instantly becomes simple, no longer need to subqueries or Join, shown in Figure 2.

2

2. FIG window function

 

    In addition, the window function compared to traditional wording, it will have better performance, we can conclude by comparing the execution plan shown in Figure 3.

3

Figure 3. By comparing the execution plan, see the window function has better performance

 

    If we consider a more complex example, in Over clause added Order By, to complete a cumulative average, if you do not use window function, it must be cursor, circulation and other troublesome way, if you use a window function, then all it becomes very easy, as shown in Figure 4.

4

4. FIG window function

 

Partition By

    Listing 2 shows the syntax window function, after which the first clause Over mention is the Partition By. Partition By clause may also be referred to as a query partition clause, very similar to Group By, according to the boundary value are the data packet, and the function performed in the previous Over each of the packet, if the packet is exceeded, then the function Re calculation, such as the example in Figure 2, we divide the data into two parts, male and female, in front of the Count () function for these two groups were calculated (206 males, 84 females).

   针对Partition By可以应用的函数不仅仅是我们所熟知的聚合函数,以及一些其他的函数,比如说Row_Number()。

 

Order By

    Order By子句是另一类子句,会让输入的数据强制排序(文章前面提到过,窗口函数是SQL语句最后执行的函数,因此可以把SQL结果集想象成输入数据)。Order By子句对于诸如Row_Number(),Lead(),LAG()等函数是必须的,因为如果数据无序,这些函数的结果就没有任何意义。因此如果有了Order By子句,则Count(),Min()等计算出来的结果就没有任何意义。

    下面我们看一个很有代表性的ROW_NUMBER()函数,该函数通常被用于分页,该函数从1开始不断递增,可以和Partition By一起使用,当穿越分区边界时,Row_Number重置为1,一个简单的例子如图5所示,我们根据请假小时数对员工进行排序。

5

图5.Row_Number函数示例

 

    另一个比较有趣的分析函数是LEAD()和LAG(),这两个分析函数经过Order By子句排序后,可以在当前行访问上N行(LAG)或下N行(LEAD)的数据,下面是一个例子,如图6所示。

6

图6.访问上一行的LAG函数

 

    另一个分析函数是RANK函数,与Row_Number不同的是,Rank函数中如果出现了相同的值,不会像Row_Number那样叠加计数,而是同样的值计数一样,比如说 1 1 3 4 5 5 7,而不是Row_Number的1 2 3 4 5 6 7。这里就不细说了。另外如果希望并列排名的不影响下一个排名,则考虑使用Dense_Rank函数。有关其他的诸如First_value和Last_Value之类的函数可以参看:http://technet.microsoft.com/zh-cn/library/hh213234.aspx

 

窗口子句

    Scope is a function of the entire table in front of the window, or the entire back of the Partition by the partition. But we can use the window clause control input to the data set window function (as I said before, the window function is the entire statement last executed) range. Here we begin to see from an example, if I want to find a company every level of sick leave up to people, we can execute in Figure 7 statement.

7

Figure 7. find most people leave each level

 

    But if we want to input data set by a particle size becomes finer Partition, we can use the window clause, so that only the window function according to the first N rows and the N rows calculation result of the current row, then we can use the window clause after shown in FIG. 8, FIG 8 we sort only three men to calculate the longest time among the sick person under the front row and the back row of the current row and the current row.

8

Figure 8. vacation time to find the person with the longest in three lines

 

    We can also use a specified range Range to the Partition, for example, we wish to find the first line, is used as shown in Fig. 9 uses the current and the previous row.

9

9.

 

summary

    From the three parts consisting of a window function simply introduces the concept of a window function, and gives some examples. More may be used in the window function, may refer to the MSDN ( http://technet.microsoft.com/zh-cn/library/ms189461.aspx ). When using these functions, but also note the version requirements, many functions are only in SQL Server 2012 was only supported.

Guess you like

Origin www.cnblogs.com/VicLiu/p/11792138.html