20

Mastering T-SQL Window Functions

Embed Size (px)

Citation preview

Page 1: Mastering T-SQL Window Functions
Page 2: Mastering T-SQL Window Functions
Page 3: Mastering T-SQL Window Functions
Page 4: Mastering T-SQL Window Functions
Page 5: Mastering T-SQL Window Functions

http://msdn.microsoft.com/en-us/library/ms189461(v=sql.110).aspx

OVER (<PARTITION BY clause><ORDER BY clause><ROW or RANGE clause>)

Divide the query result set

into partitions and the

operation is applied to each

partition separately

Defines the logical order of

the rows within each partition

of the result set

Limits the rows within the

partition by specifying start

and end points within the

partition

Page 6: Mastering T-SQL Window Functions

ID AcctID TransDate TransAmt

1 1234 27/11/2012 $150.00

2 1234 27/11/2012 $22.00

3 5678 28/11/2012 $50.00

4 5678 28/11/2012 $150.00

5 5678 28/11/2012 $10.00

6 5678 29/11/2012 $120.00

7 0987 30/11/2012 $20.00

8 0987 30/11/2012 $100.00

9 0987 30/11/2012 $50.00

Aggregation Window:SUM(TransAmt) OVER(PARTITION BY TransDate)

Ranking Window:ROW_NUMBER() OVER(PARTITION BY TransDate

ORDER BY AcctID, ID)

AcctID TransDate TransAmt BalAmt

5678 28/11/2012 $50.00 $50.00

5678 28/11/2012 $150.00 $200.00

5678 28/11/2012 $10.00 $210.00

AcctID TransDate TransAmt BalAmt Rank

5678 28/11/2012 $50.00 $50.00 1

5678 28/11/2012 $150.00 $200.00 2

5678 28/11/2012 $10.00 $210.00 3

AcctID TransDate TransAmt

5678 28/11/2012 $50.00

5678 28/11/2012 $150.00

5678 28/11/2012 $10.00

Page 7: Mastering T-SQL Window Functions
Page 8: Mastering T-SQL Window Functions

Ranking Aggregation Analytic

ROW_NUMBER()

RANK()

DENSE_RANK()

NTILE()

SUM() | AVG() | COUNT()

MIN() | MAX()

CHECKSUM_AGG

STDEV() | STDEVP()

VAR() | VARP()

LEAD() | LAG()

FIRST_VALUE() | LAST_VALUE()

CUME_DIST()

PERCENT_RANK()

PERCENTILE_DIST()

PERCENTILE_CONT()

Page 9: Mastering T-SQL Window Functions

No Framing

Available

Page 10: Mastering T-SQL Window Functions

DEMO

Page 11: Mastering T-SQL Window Functions
Page 12: Mastering T-SQL Window Functions

DEMO

Page 13: Mastering T-SQL Window Functions

No Framing

Available

LEAD | LAG

(scalar_expression [,offset] [,default])

OVER ( [ partition_by_clause ]

order_by_clause )

Page 14: Mastering T-SQL Window Functions

FIRST_VALUE | LAST_VALUE

( [scalar_expression] )

OVER ( [ partition_by_clause ]

order_by_clause

rows_range_clause )

Page 15: Mastering T-SQL Window Functions

No Framing

Available

CUME_DIST()

OVER ( [ partition_by_clause ]

order_by_clause )

Page 16: Mastering T-SQL Window Functions

No Framing

Available

PERCENT_RANK()

OVER ( [ partition_by_clause ]

order_by_clause )

Page 17: Mastering T-SQL Window Functions

No Framing

Available

PERCENTILE_DIST ( numeric_literal )

WITHIN GROUP ( ORDER BY order_by_expression [ ASC | DESC ] )

OVER ( [ partition_by_clause ]

order_by_clause

rows_range_clause )

Page 18: Mastering T-SQL Window Functions

No Framing

Available

PERCENTILE_CONT ( numeric_literal )

WITHIN GROUP ( ORDER BY order_by_expression [ ASC | DESC ] )

OVER ( [ partition_by_clause ]

order_by_clause

rows_range_clause )

Page 19: Mastering T-SQL Window Functions

DEMO

Page 20: Mastering T-SQL Window Functions