360 likes | 685 Views
T-SQL Window Function Deep Dive part 1. Kathi Kellenberger @ auntkathi Kathi.kellenberger@red-gate.com http://auntkathisql.com. What are T-SQL window functions? 2005 Ranking functions Window aggregates 2012 Enhancements Accumulating window aggregates Framing Offset functions
E N D
T-SQL Window Function Deep Dive part 1 Kathi Kellenberger @auntkathi Kathi.kellenberger@red-gate.com http://auntkathisql.com
What are T-SQL window functions? 2005 Ranking functions Window aggregates 2012 Enhancements Accumulating window aggregates Framing Offset functions Statistical functions Agenda
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 PartitionsNOT the same as GROUP BY
Ranking functions Available since 2005 ROW_NUMBER() A unique # over the window RANK() Deals with ties DENSE_RANK() Deals with ties NTILE() Divide rows into buckets ORDER BY is required in OVER clause
Demo 1 Ranking functions
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
Demo 2 Window aggregates
2012 enhancements Accumulation aggregates Even further define the window with FRAMING Eight new functions Offset Statistical
Demo 3 Accumulating aggregates
FRAMING: ROWS and RANGE Further define the window Each row has its own window Supported for specific function types
FRAMING ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ROWS UNBOUNDED PRECEDING
FRAMING ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
FRAMING ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
Demo 4 Framing
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!!
Demo 5 Offset functions
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
PERCENT_RANK example 25, 50, 75, 100
CUME_DIST example 25, 50, 75, 100
PERCENTILE_DISC example 0.5 Percentile_Disc = 50
PERCENTILE_CONT example 0.5 Percentile_cont = 62.5
Demo 6 Statistical functions
My book: Expert T-SQL Window Functions Itzik Ben-Gan’s book: Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions http://www.auntkathisql.com My Pluralsight course Resources
Just like Jimi Hendrix … We love to get feedback Please complete the session feedback forms
SQLBits - It's all about the community... Please visit Community Corner, we are trying this year to get more people to learn about the SQL Community, equally if you would be happy to visit the community corner we’d really appreciate it.