Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Window Functions

Similar presentations


Presentation on theme: "Introduction to Window Functions"— Presentation transcript:

1 Introduction to Window Functions
Kathi Kellenberger, MVP Linchpin People auntkathisql.com

2 History 2005: Window functions introduced 2012: Window functions enhanced

3 Agenda What are window functions? Ranking functions Window aggregates
2012 Enhancements: ORDER BY 2012 Enhancements: Framing Analytic Functions Offset Functions Statistical Functions Making Business Intelligent

4 What are window functions?
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

5 Ranking functions Available since 2005 ROW_NUMBER()
A unique # over the window RANK() Deals with ties DENSE_RANK() NTILE() Divide rows into buckets

6 ROW_NUMBER() example CustomerID OrderID Total
Row_Number() Over(Order by OrderID) 1 101 100 2 102 40 103 11 3 104 432 4 105 2000 5 106 300 6 107 674 7 108 76 8 109 234 9 110 889 10

7 ROW_NUMBER() example CustomerID OrderID TotalDue
Row_Number() Over(Partition by CustomerID Order by OrderID) 1 100 5 2000 2 6 300 3 40 11 4 432 7 674 9 234 10 889 8 76

8 Ranking Functions Demo

9 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 Pre-2012 functionality NOT optimized for performance

10 Window aggregate example
CustomerID OrderID TotalDue Sum(TotalDue) Over() 1 100 4990 5 2000 6 300 2 40 3 11 4 432 7 674 9 234 10 889 8 76

11 Window aggregate example
CustomerID OrderID TotalDue Sum(TotalDue) Over(Partition by CustomerID) 1 100 2400 5 2000 6 300 2 40 51 3 11 4 432 7 674 1797 9 234 10 889 8 76 310

12 Window Aggregates Demo

13 2012 enhancements ORDER BY clause added to window aggregates Performance optimization Even further define the window with ROWS and RANGE: FRAMING

14 Window aggregate ORDER BY example
CustomerID OrderID TotalDue Sum(TotalDue) Over(Order by OrderID) 1 100 2 40 140 3 11 151 4 432 583 5 2000 2583 6 300 2883 7 674 3557 8 76 3633 9 234 3867 10 889 4756 4990

15 Running Total Demo

16 FRAME: ROWS and RANGE Further define window
Terms UNBOUNDED PRECEDING UNBOUNDED FOLLOWING CURRENT ROW # PRECEDING # FOLLOWING RANGE is not fully implemented and can hurt performance

17 Framing Rows between unbounded preceding and current row

18 Framing Rows between current row and unbounded following

19 Framing Rows between 2 preceding and current row

20 Framing Rows between 5 preceding and 2 following

21 Framing Demo

22 The Analytic Functions
LAG() and LEAD() Look back or look forward FIRST_VALUE() and LAST_VALUE() The very first or very last PERCENT_RANK() and CUME_DIST() Calculate ranking PERCENTILE_DISC() and PERCENTILE_CONT() Given a percent, find the value

23 Analytic Functions Demo

24 Resources


Download ppt "Introduction to Window Functions"

Similar presentations


Ads by Google