Upload
mohammad-rojas
View
34
Download
3
Embed Size (px)
DESCRIPTION
Writing Better Queries with Window Functions. Who am I?. Kathi Kellenberger, MVP Pragmatic Works www.bidn.com/blogs/kathikellenberger [email protected] @ auntkathi. Teaching SSRS and SSRS Master Classes. - PowerPoint PPT Presentation
Citation preview
1
Writing Better Queries with Window Functions
2
Who am I?
Kathi Kellenberger, MVPPragmatic Works
www.bidn.com/blogs/[email protected]
@auntkathi
Teaching SSRS and SSRS Master Classes
Beginning SSRS Joes 2 ProsBeginning T-SQL 2012Beginning T-SQL 2008SQL Server MVP Deep Dives (Vol 1)
3
History
2005: Window functions introduced
2012: Window functions enhanced
Best resource: Microsoft SQL Server2012 High-Performance T-SQL Using Window Functions by
Itzik Ben-Gan
Agenda
• What are window functions?
• Ranking functions
• Window aggregates
• 2012 Enhancements: ORDER BY
• 2012 Enhancements: Framing
• Analytic functions
MAKING BUSINESS INTELLIGENT
5
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 functionsSELECT 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
6
Ranking functions
Available since 2005ROW_NUMBER()
A unique # over the window
RANK()
Deals with ties
DENSE_RANK()
Deals with ties
NTILE()
Divide rows into buckets
ROW_NUMBER() example
CustomerID
OrderID
Total Row_Number() Over(Order by OrderID)
1 101 100 1
2 102 40 2
2 103 11 3
3 104 432 4
1 105 2000 5
1 106 300 6
4 107 674 7
5 108 76 8
4 109 234 9
4 110 889 10
5 110 234 11
ROW_NUMBER() example
CustomerID OrderID TotalDue
Row_Number() Over(Partition by CustomerID Order by OrderID)
1 1 100 1
1 5 2000 2
1 6 300 3
2 2 40 1
2 3 11 2
3 4 432 1
4 7 674 1
4 9 234 2
4 10 889 3
5 8 76 1
5 11 234 2
9
Ranking functions: Tuning
Sequence Project Operator
Index: Partition + Order by + Covering
If there is a filter, add filter columns first
Watch out for reverse directionor different ordering
Ranking Functions
Demo
11
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
Window aggregate example
CustomerID
OrderID
TotalDue
Sum(TotalDue) Over()
1 1 100 4990
1 5 2000 4990
1 6 300 4990
2 2 40 4990
2 3 11 4990
3 4 432 4990
4 7 674 4990
4 9 234 4990
4 10 889 4990
5 8 76 4990
5 11 234 4990
Window aggregate example
CustomerID OrderID TotalDue
Sum(TotalDue) Over(Partition by CustomerID)
1 1 100 2400
1 5 2000 2400
1 6 300 2400
2 2 40 51
2 3 11 51
3 4 432 432
4 7 674 1797
4 9 234 1797
4 10 889 1797
5 8 76 310
5 11 234 310
Window Aggregates
Demo
15
2012 enhancements
ORDER BY clauseadded to window aggregates
Performance optimization
Even further define the window with ROWS and RANGE: FRAMING
Window aggregate ORDER BY example
CustomerID
OrderID
TotalDue Sum(TotalDue) Over(Order by OrderID)
1 1 100 100
2 2 40 140
2 3 11 151
3 4 432 583
1 5 2000 2583
1 6 300 2883
4 7 674 3557
5 8 76 3633
4 9 234 3867
4 10 889 4756
5 11 234 4990
Running Total
Demo
18
FRAME: ROWS and RANGE
Further define window
TermsUNBOUNDED PRECEDINGUNBOUNDED FOLLOWINGCURRENT ROW# PRECEDING# FOLLOWING
RANGE is not fully implementedand can hurt performance
19
Framing
Rows between unbounded preceding and current row
20
Framing
Rows between current row and unbounded following
21
Framing
Rows between 2 preceding and current row
22
Framing
Rows between 5 precedingand 2 following
Framing
Demo
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
Analytic Functions
Demo