25
1 Writing Better Queries with Window Functions

Writing Better Queries with Window Functions

  • 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

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 ClassesBeginning 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 windowRANK() Deals with tiesDENSE_RANK() Deals with tiesNTILE() 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 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

Page 8: Writing Better Queries with  Window Functions

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

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 BYAdd aggregate function to a non-aggregate queryCalculate over the window: the entire result set or

partitionPre-2012 functionality NOT optimized for

performance

Page 12: Writing Better Queries with  Window Functions

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

Page 13: Writing Better Queries with  Window Functions

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

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 optimizationEven 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 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

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 Terms

UNBOUNDED 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