40

Windowing functions - Kevin Boles

Embed Size (px)

DESCRIPTION

Presentation from Kevin Boles at SQL In The City US Tour 2013.

Citation preview

Page 1: Windowing functions - Kevin Boles
Page 2: Windowing functions - Kevin Boles

Page 3: Windowing functions - Kevin Boles

•–

Page 4: Windowing functions - Kevin Boles

Page 5: Windowing functions - Kevin Boles
Page 6: Windowing functions - Kevin Boles

•–

Page 7: Windowing functions - Kevin Boles
Page 8: Windowing functions - Kevin Boles

Page 9: Windowing functions - Kevin Boles

Page 10: Windowing functions - Kevin Boles

• PARTITION BY -

• ORDER BY -

• ROWS | RANGE -

OVER( [ <PARTITION BY clause> ]

[ <ORDER BY clause> ]

[ <ROW or RANGE clause> ] )

Page 11: Windowing functions - Kevin Boles
Page 12: Windowing functions - Kevin Boles

Page 13: Windowing functions - Kevin Boles

Page 14: Windowing functions - Kevin Boles

Page 15: Windowing functions - Kevin Boles
Page 16: Windowing functions - Kevin Boles
Page 17: Windowing functions - Kevin Boles

Page 18: Windowing functions - Kevin Boles

• scalar_expression –

• OVER –

LAST_VALUE(scalar_expression)

OVER ([partition_by_clause] order_by_clause)

Page 19: Windowing functions - Kevin Boles

Page 20: Windowing functions - Kevin Boles

SELECT OrderID, CustomerID,

FIRST_VALUE(OrderID) OVER(PARTITION BY CustomerID ORDER BY OrderID

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)

AS FirstOrderID

FROM dbo.Orders

WHERE CustomerID IN (1,2)

Page 21: Windowing functions - Kevin Boles

SELECT OrderID, CustomerID,

LAST_VALUE(OrderID) OVER(PARTITION BY CustomerID ORDER BY OrderID

ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

AS FirstOrderID

FROM dbo.Orders

WHERE CustomerID IN (1,2)

Page 22: Windowing functions - Kevin Boles
Page 23: Windowing functions - Kevin Boles

SELECT st1.[Date], st1.TicketCount,RunningTotal = SUM(st2.TicketCount)

FROM dbo.SpeedingTickets AS st1 INNER JOIN dbo.SpeedingTickets AS st2

ON st2.[Date] <= st1.[Date] GROUP BY st1.[Date], st1.TicketCount ORDER BY st1.[Date];

From Aaron Bertrand

Page 24: Windowing functions - Kevin Boles

SELECT [Date], TicketCount, RunningTotal = TicketCount +

COALESCE(( SELECT SUM(TicketCount) FROM dbo.SpeedingTickets AS s

WHERE s.[Date] < o.[Date]), 0 )FROM dbo.SpeedingTickets AS o ORDER BY [Date];

Page 25: Windowing functions - Kevin Boles
Page 26: Windowing functions - Kevin Boles
Page 27: Windowing functions - Kevin Boles
Page 28: Windowing functions - Kevin Boles

Page 29: Windowing functions - Kevin Boles

• scalar_expression –

• Offset –

• Default –

• OVER –

LAG (scalar_expression [,offset] [,default])

OVER ([partition_by_clause] order_by_clause)

Page 30: Windowing functions - Kevin Boles

Page 31: Windowing functions - Kevin Boles
Page 32: Windowing functions - Kevin Boles
Page 33: Windowing functions - Kevin Boles

CUME_DIST ( ) OVER

([ partition_by_clause ] order_by_clause)

Page 34: Windowing functions - Kevin Boles

PERCENT_RANK ( ) OVER

([ partition_by_clause ] order_by_clause)

Page 35: Windowing functions - Kevin Boles

Page 36: Windowing functions - Kevin Boles

PERCENTILE_DISC ( numeric_literal) WITHIN GROUP ( ORDER BY expression) OVER ([<partition_by_clause> ] )

Page 37: Windowing functions - Kevin Boles

PERCENTILE_CONT ( numeric_literal) WITHIN GROUP ( ORDER BY expression) OVER ([<partition_by_clause> ] )

Page 38: Windowing functions - Kevin Boles

Page 39: Windowing functions - Kevin Boles
Page 40: Windowing functions - Kevin Boles

Deck by Shy Engelberg and Kevin Boles