25
1 Writing Better Queries with Window Functions

Writing Better Queries with Window Functions

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

Page 1: Writing Better Queries with  Window Functions

1

Writing Better Queries with Window Functions

Page 2: 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)

Page 3: Writing Better Queries with  Window Functions

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

Page 4: Writing Better Queries with  Window Functions

Agenda

• What are window functions?

• Ranking functions

• Window aggregates

• 2012 Enhancements: ORDER BY

• 2012 Enhancements: Framing

• Analytic functions

MAKING BUSINESS INTELLIGENT

Page 5: Writing Better Queries with  Window Functions

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

Page 6: Writing Better Queries with  Window Functions

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

Page 7: Writing Better Queries with  Window 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 110 234 11

Page 8: Writing Better Queries with  Window Functions

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

Page 9: Writing Better Queries with  Window Functions

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

Page 10: Writing Better Queries with  Window Functions

Ranking Functions

Demo

Page 11: Writing Better Queries with  Window Functions

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

Page 12: Writing Better Queries with  Window Functions

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

Page 13: Writing Better Queries with  Window Functions

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

Page 14: Writing Better Queries with  Window Functions

Window Aggregates

Demo

Page 15: Writing Better Queries with  Window Functions

15

2012 enhancements

ORDER BY clauseadded to window aggregates

Performance optimization

Even further define the window with ROWS and RANGE: FRAMING

Page 16: Writing Better Queries with  Window Functions

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

Page 17: Writing Better Queries with  Window Functions

Running Total

Demo

Page 18: Writing Better Queries with  Window Functions

18

FRAME: ROWS and RANGE

Further define window

TermsUNBOUNDED PRECEDINGUNBOUNDED FOLLOWINGCURRENT ROW# PRECEDING# FOLLOWING

RANGE is not fully implementedand can hurt performance

Page 19: Writing Better Queries with  Window Functions

19

Framing

Rows between unbounded preceding and current row

Page 20: Writing Better Queries with  Window Functions

20

Framing

Rows between current row and unbounded following

Page 21: Writing Better Queries with  Window Functions

21

Framing

Rows between 2 preceding and current row

Page 22: Writing Better Queries with  Window Functions

22

Framing

Rows between 5 precedingand 2 following

Page 23: Writing Better Queries with  Window Functions

Framing

Demo

Page 24: Writing Better Queries with  Window Functions

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

Page 25: Writing Better Queries with  Window Functions

Analytic Functions

Demo