Writing Better Queries with Window Functions Kathi Kellenberger, MVP Linchpin People auntkathisql.com
History 2005: Window functions introduced 2012: Window functions enhanced
Agenda What are window functions? Ranking functions Window aggregates 2012 Enhancements: ORDER BY 2012 Enhancements: Framing Eight new functions Making Business Intelligent
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
Ranking functions Available since 2005 ROW_NUMBER() A unique # over the window RANK() Deals with ties DENSE_RANK() NTILE() Divide rows into buckets
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
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
Ranking Functions Demo
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 2005 functionality NOT optimized for performance
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
Window Aggregates Demo
2012 enhancements ORDER BY clause added to window aggregates Even further define the window with ROWS and RANGE: FRAMING Eight new functions
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
Running Total Demo
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
Framing Rows between unbounded preceding and current row
Framing Rows between current row and unbounded following
Framing Rows between 2 preceding and current row
Framing Rows between 5 preceding and 2 following
Framing Demo
Cultivating a Healthy Database Office Hours June 24 2015 at 12:00 ET http://tiny.cc/dkb9yx Text LINCHPIN to 33444 Linchpin People is a team of amazing experts focused entirely on the Microsoft database ecosystem Linchpin People is comprised of a team of world class SQL Server Experts and MVPs who work together to create and deliver top-of-the-line Microsoft Data Ecosystem solutions. We can help you design new projects that are on the drawing board, Or we can rescue projects that are already being implemented but may need some expert assistance to successfully meet their intended goals. Our expertise spans the Microsoft ecosystem - from SQL Server based performance tuning, high availability and disaster recovery, data integration architecture, ETL and data warehouse consulting, big data, complex data, business intelligence and analytics, to platform agnostic application development with a data-centric focus. Have a problem in this space? We can help.
The New 2012 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
Analytic Functions Demo