T-SQL WindowingFunctionsDeep DiveKathi Kellenberger, Teammate
Linchpin People
Session Evaluations
ways to access
Go to
passsummit.com/evals
Download the GuideBook App
and search: PASS Summit 2014
Follow the QR code link displayed
on session signage throughout the
conference venue and in the
program guide
Submit by 11:59 PM ESTFriday Nov. 7 toWIN prizes
Your feedback is important and valuable.
Evaluation Deadline:
11:59 PM EST, Sunday Nov. 16
Agenda
• Part 1: Overview of T-SQL Windowing Functions
• BREAK
• Part 2: Real World Examples
• Part 3: Query Tuning and Performance
Agenda
• Part 1: Overview of T-SQL Windowing Functions• What are Windowing Functions?
• 2005 Features• Ranking
• Window Aggregates
• 2012 Enhancements• Framing
• Running totals
• Analytic functions
• Part 2: Real World Examples
• Part 3: Query Tuning and Performance
Nothing to do with OS
Functions operate on a set, or window
Found only in SELECT and ORDER BY
OVER clause defines the window
Partitions – not the same as GROUP BY
What are Windowing Functions?
ROW_NUMBER()A unique number over the window
RANK()Repeats number if ties
DENSE_RANK()Repeats number if ties
NTILE()Divides data into buckets
Ranking Functions
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 111 234 11
ROW_NUMBER() Example - Partitioning
CustomerID OrderID TotalROW_NUMBER() OVER(PARTITION BY CustomerID
ORDER BY OrderID)
1 101 100 1
1 105 2000 2
1 106 300 3
2 102 40 1
2 103 11 2
3 104 432 1
4 107 674 1
4 109 234 2
4 110 889 3
5 108 76 1
5 111 234 2
Ranking FunctionsDemo
Your favorite aggregate functions
with no GROUP BY
Add aggregate functions to
non-aggregate query
Return details
Window Aggregates
Window Aggregate Example
CustomerID OrderID Total SUM(Total) OVER()
1 101 100 4990
2 102 40 4990
2 103 11 4990
3 104 432 4990
1 105 2000 4990
1 106 300 4990
4 107 674 4990
5 108 76 4990
4 109 234 4990
4 110 889 4990
5 111 234 4990
Window Aggregate Example -Partitioning
CustomerID OrderID Total SUM(Total) OVER(PARTITION BY CustomerID)
1 101 100 2400
1 105 2000 2400
1 106 300 2400
2 102 40 51
2 103 11 51
3 104 432 432
4 107 674 1797
4 109 234 1797
4 110 889 1797
5 108 76 310
5 111 234 310
Window AggregatesDemo
Framing
ORDER BY and Framing
to Window Aggregates
Analytic functions
2012 Enhancements
Further define window
TermsUnbounded Preceding
Unbounded Following
Current Row
RANGE not fully implementedAlmost like ROWS, is the default
Framing: ROWS and RANGE
ROWS BETWEEN UNBOUNDED
PRECEDING AND
CURRENT ROW
ROWS UNBOUNDED
PRECEDING
Framing
ROWS BETWEEN CURRENT
ROW AND UNBOUNDED
FOLLOWING
Framing
ROWS BETWEEN 2 PRECEDING AND
AND CURRENT ROW
Framing
ROWS BETWEEN 5 PRECEDING AND
AND 2 FOLLOWING
Framing
CustomerID OrderID Total UNBOUNDED PRECEDING AND CURRENT ROW
1 101 100
2 102 40
2 103 11
3 104 432
1 105 2000
1 106 300
4 107 674
5 108 76
4 109 234
4 110 889
5 111 234
Framing Example
CustomerID OrderID Total SUM(Total) OVER(ORDER BY OrderID)
1 101 100 100
2 102 40 140
2 103 11 151
3 104 432 583
1 105 2000 2583
1 106 300 2883
4 107 674 3557
5 108 76 3633
4 109 234 3867
4 110 889 4756
5 111 234 4990
Framing Example
Running TotalsDemo
LAG() and LEAD()
FIRST_VALUE() and LAST_VALUE()
PERCENT_RANK() and
CUME_DIST()
PERCENTILE_DISC and
PERCENTILE_CONT
Analytic Functions
CustomerID OrderID Total LAG(Total) OVER(ORDER BY OrderID)
1 101 100 NULL
2 102 40 100
2 103 11 40
3 104 432 11
1 105 2000 432
1 106 300 2000
4 107 674 300
5 108 76 674
4 109 234 76
4 110 889 234
5 111 234 889
LAG() Example
CustomerID OrderID Total LEAD(Total) OVER(ORDER BY OrderID)
1 101 100 40
2 102 40 11
2 103 11 432
3 104 432 2000
1 105 2000 300
1 106 300 674
4 107 674 76
5 108 76 234
4 109 234 889
4 110 889 234
5 111 234 NULL
LEAD() Example
CustomerID OrderID TotalFIRST_ROW(Total) OVER(ORDER BY
OrderID)
1 101 100 100
2 102 40 100
2 103 11 100
3 104 432 100
1 105 2000 100
1 106 300 100
4 107 674 100
5 108 76 100
4 109 234 100
4 110 889 100
5 111 234 100
FIRST_ROW()* Example
*Frame required!
CustomerID OrderID TotalLAST_ROW(Total) OVER(ORDER BY
OrderID)
1 101 100 234
2 102 40 234
2 103 11 234
3 104 432 234
1 105 2000 234
1 106 300 234
4 107 674 234
5 108 76 234
4 109 234 234
4 110 889 234
5 111 234 234
LAST_ROW()* Example
*Frame required!
LAG, LEAD, FIRST_VALUE, LAST_VALUE
Demo
Average High Temp by Month STL
40 4355
6777
85 89 8881
6956
43
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
What is the Percent Rank?
40 43 4355 56
67 6977 81 85 88 89
Jan Feb Dec Mar Nov Apr Oct May Sept Jun Aug Jul
Rank = 4
Percent Rank = 27% (4-1)/(12-1)
Cumulative Distribution = 33% (4)/12
What is the value at 50%, the median?
40 43 4355 56
67 6977 81 85 88 89
Jan Feb Dec Mar Nov Apr Oct May Sept Jun Aug Jul
Percentile Cont = 68 (67 + 69)/2
Percentile Disc = 67
Percent FunctionsDemo
Agenda
• Part 1: Overview of T-SQL Windowing Functions
• Part 2: Real World Examples• Islands and Gaps
• Fun with Row_Number
• Fun with window aggregates
• The stock problem
• Interesting ideas…
• Part 3: Query Tuning and Performance
What is the Islands and Gaps Problem?
1,2,3,4,5,8,9,13,14,16,17
Real World…Demo
Session Evaluations
ways to access
Go to
passsummit.com/evals
Download the GuideBook App
and search: PASS Summit 2014
Follow the QR code link displayed
on session signage throughout the
conference venue and in the
program guide
Submit by 11:59 PM ESTFriday Nov. 7 toWIN prizes
Your feedback is important and valuable.
Evaluation Deadline:
11:59 PM EST, Sunday Nov. 16
Agenda
• Part 1: Overview of T-SQL Windowing Functions
• Part 2: Real World Examples
• Part 3: Query Tuning and Performance• Execution plan operators
• Indexing
• Framing
• Large table comparisons
Execution Plan Operators
POC Index
Columns needed by WHERE
Partition by column
Order by column
Include Covering columns
Indexing
Query Tuning Part 1Demo
Default frame:
RANGE BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
Specify ROWS for better performance
Subtle logic differences
Framing: Running Aggregates, Last_value, First_value
Query Tuning Part 2Demo
Subtotals – 30 million rows
1.75
0.50 0.5
1.75
0.500.75
1.75
0.501
Window Aggregate CTE Correlated Subquery
1 Calc 2 Calcs 3 Calcs
Running Totals 7.5, 15, and 30 million rows
2 04 64
0.5
17
118
1
50.5
21.5
WF Default Frame WF Rows Join/SQ Cursor
7.5 M 15 M 30 M
Big Adventure Script:
http://sqlblog.com/blogs/adam_machanic/archive/2011/10
/17/thinking-big-adventure.aspx
Itzik Ben-Gan’s book: Microsoft SQL Server 2012 High-
Performance T-SQL Using Window Functions
My blog: http://auntkathisql.com
My Book: Beginning T-SQL (Apress, 2014)
Resources
Session Evaluations
ways to access
Go to
passsummit.com/evals
Download the GuideBook App
and search: PASS Summit 2014
Follow the QR code link displayed
on session signage throughout the
conference venue and in the
program guide
Submit by 11:59 PM ESTFriday Nov. 7 toWIN prizes
Your feedback is important and valuable.
Evaluation Deadline:
11:59 PM EST, Sunday Nov. 16