Upload
roxy
View
35
Download
0
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 ClassesBeginning 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 windowRANK() Deals with tiesDENSE_RANK() Deals with tiesNTILE() Divide rows into buckets
ROW_NUMBER() example
CustomerID
OrderID
Total Row_Number() Over(Order by OrderID)
1 101 100 12 102 40 22 103 11 33 104 432 41 105 2000 51 106 300 64 107 674 75 108 76 84 109 234 94 110 889 105 110 234 11
ROW_NUMBER() exampleCustomerID OrderID TotalDu
eRow_Number() Over(Partition by CustomerID Order by OrderID)
1 1 100 11 5 2000 21 6 300 32 2 40 12 3 11 23 4 432 14 7 674 14 9 234 24 10 889 35 8 76 15 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 BYAdd aggregate function to a non-aggregate queryCalculate over the window: the entire result set or
partitionPre-2012 functionality NOT optimized for
performance
Window aggregate exampleCustomerID
OrderID
TotalDue
Sum(TotalDue) Over()
1 1 100 49901 5 2000 49901 6 300 49902 2 40 49902 3 11 49903 4 432 49904 7 674 49904 9 234 49904 10 889 49905 8 76 49905 11 234 4990
Window aggregate exampleCustomerID OrderID TotalDu
eSum(TotalDue) Over(Partition by CustomerID)
1 1 100 24001 5 2000 24001 6 300 24002 2 40 512 3 11 513 4 432 4324 7 674 17974 9 234 17974 10 889 17975 8 76 3105 11 234 310
Window Aggregates
Demo
15
2012 enhancements
ORDER BY clauseadded to window aggregates
Performance optimizationEven 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 1002 2 40 1402 3 11 1513 4 432 5831 5 2000 25831 6 300 28834 7 674 35575 8 76 36334 9 234 38674 10 889 47565 11 234 4990
Running Total
Demo
18
FRAME: ROWS and RANGE
Further define window Terms
UNBOUNDED 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