Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to T-sql Window functionS

Similar presentations


Presentation on theme: "Introduction to T-sql Window functionS"— Presentation transcript:

1 Introduction to T-sql Window functionS
Kathi Kellenberger @auntkathi

2 Agenda What are T-SQL window functions? 2005 Ranking functions
Window aggregates 2012 Enhancements Accumulating window aggregates Framing Offset functions Statistical functions Agenda

3 What are window functions?
Not OS, based on ANSI-SQL standards Function performs over a SET (window of the results) How to identify a window function The OVER clause defines the window Where to put window functions SELECT and ORDER BY clauses only Important! The FROM, WHERE, GROUP BY and HAVING clauses operate BEFORE the window functions Partitions NOT the same as GROUP BY

4 Ranking functions Available since 2005
ROW_NUMBER() A unique # over the window RANK() Deals with ties DENSE_RANK() NTILE() Divide rows into buckets ORDER BY is required in OVER clause

5 ROW_NUMBER() example CustomerID OrderID Total
ROW_NUMBER() OVER(PARTITION BY CustomerID Order by OrderID) 1 101 100 105 2000 2 106 300 3 102 40 103 11 104 432 4 107 674 109 234 110 889 5 108 76 111

6 Demo 1 Ranking functions

7 Window aggregate functions
Your favorite aggregates with no GROUP BY Add aggregate function to a non-aggregate query Calculate over the window: the entire result set or partition No ORDER BY Partition by OR use the Empty Over Clause

8 Window Aggregate example
CustomerID OrderID TotalDue SUM(TotalDue) OVER(Partition by CustomerID) 1 101 100 2400 102 2000 103 300 2 104 40 51 105 11 3 106 432 4 107 674 1797 108 234 109 889 5 110 76 310 111

9 Demo 2 Window aggregates

10 2012 enhancements Accumulation aggregates Even further define the window with FRAMING Eight new functions Offset Statistical

11 Accumulating aggregate example
CustomerID OrderID TotalDue SUM(TotalDue) OVER(Order by OrderID) 1 101 100 2 102 40 140 103 11 151 3 104 432 583 105 2000 2583 106 300 2883 4 107 674 3557 5 108 76 3633 109 234 3867 110 889 4756 111 4990

12 Accumulating aggregates
Demo 3 Accumulating aggregates

13 FRAMING: ROWS and RANGE
Further define the window Each row has its own window Supported for specific function types

14 Framing: Vocabulary Term Used for ROWS
Positional operator used to define the frame RANGE Logical operator used to define the frame. Not fully supported. The default if ROWS is not specified UNBOUNDED PRECEDING The first row of the partition UNBOUNDED FOLLOWING The last row of the partition CURRENT ROW The row where the calculation is being performed

15 FRAMING ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ROWS UNBOUNDED PRECEDING

16 FRAMING ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING

17 FRAMING ROWS BETWEEN 2 PRECEDING AND CURRENT ROW 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW

18 Demo 4 Framing

19 OFFSET Functions LAG() Grab a column from a previous row LEAD() Grab a column from a later row FIRST_VALUE() Grab a column from the first row Supports framing LAST_VALUE() Grab a column from the last row Supports framing -- be careful!!

20 LAG example CustomerID OrderID TotalDue
LAG(TotalDue) OVER(ORDER BY OrderID) 1 101 100 NULL 2 102 40 103 11 3 104 432 105 2000 106 300 4 107 674 5 108 76 109 234 110 889 111

21 LEAD example CustomerID OrderID TotalDue
LEAD(TotalDue) OVER(ORDER BY OrderID) 1 101 100 40 2 102 11 103 432 3 104 2000 105 300 106 674 4 107 76 5 108 234 109 889 110 111 NULL

22 First_VALUE example CustomerID OrderID TotalDue
FIRST_VALUE (TotalDue) OVER(ORDER BY OrderID) 1 101 100 2 102 40 103 11 3 104 432 105 2000 106 300 4 107 674 5 108 76 109 234 110 889 111

23 LAST_VALUE example CustomerID OrderID TotalDue
LAST_VALUE(TotalDue) OVER(ORDER BY OrderID) 1 101 100 234 2 102 40 103 11 3 104 432 105 2000 106 300 4 107 674 5 108 76 109 110 889 111

24 Demo 5 Offset functions

25 Statistical functions
PERCENT_RANK() Relative rank. “My score is better than 90% of the scores” CUME_DIST() Cumulative distribution over a group of rows. “My score is at 90%” PERCENTILE_DISC() Computes a specific percentile PERCENTILE_CONT() Computes an exact percentile

26 PERCENT_RANK example

27 CUME_DIST example

28 PERCENTILE_DISC example

29 PERCENTILE_CONT example

30 Statistical functions
Demo 6 Statistical functions

31 Resources My book: Expert T-SQL Window Functions
Itzik Ben-Gan’s book: Microsoft SQL Server High-Performance T-SQL Using Window Functions My Pluralsight course


Download ppt "Introduction to T-sql Window functionS"

Similar presentations


Ads by Google