Window Functions

advertisement
Writing Better Queries with
Window Functions
1
Who am I?
Kathi Kellenberger, MVP
Pragmatic Works
www.bidn.com/blogs/kathikellenberger
kkellenberger@pragmaticworks.com
@auntkathi
Teaching SSRS and SSRS Master Classes
Beginning SSRS Joes 2 Pros
Beginning T-SQL 2012
Beginning T-SQL 2008
SQL Server MVP Deep Dives (Vol 1)
2
History
2005: Window functions introduced
2012: Window functions enhanced
Best resource: Microsoft SQL Server
2012 High-Performance T-SQL
Using Window Functions by
Itzik Ben-Gan
3
Agenda
•
What are window functions?
•
Ranking functions
•
Window aggregates
•
2012 Enhancements: ORDER BY
•
2012 Enhancements: Framing
•
Analytic 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
5
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
6
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
Ranking functions: Tuning
Sequence Project Operator
Index: Partition + Order by +
Covering
If there is a filter,
add filter columns first
Watch out for reverse direction
or different ordering
9
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
Pre-2012 functionality NOT optimized for performance
11
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
2012 enhancements
ORDER BY clause
added to window
aggregates
Performance optimization
Even further define the
window with ROWS
and RANGE: FRAMING
15
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
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
18
Framing
Rows between unbounded
preceding and current row
19
Framing
Rows between current row
and unbounded following
20
Framing
Rows between 2 preceding and
current row
21
Framing
Rows between 5 preceding
and 2 following
22
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
Download