Windowing functions - Kevin Boles

483 views

Published on

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

Published in: Technology, Spiritual
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
483
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Windowing functions - Kevin Boles

  1. 1. • • • • •
  2. 2. • • • • • – 
  3. 3. • • • • • •
  4. 4. • • – – – – – – – – –
  5. 5. • • • – – – –
  6. 6.
  7. 7. OVER( [ <PARTITION BY clause> ] [ <ORDER BY clause> ] [ <ROW or RANGE clause> ] ) • PARTITION BY • ORDER BY • ROWS | RANGE -
  8. 8. • •
  9. 9. • • •
  10. 10. • – – – – – –
  11. 11. • •
  12. 12. LAST_VALUE(scalar_expression) OVER ([partition_by_clause] order_by_clause) • scalar_expression – • OVER –
  13. 13. • – – • –
  14. 14. 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)
  15. 15. 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)
  16. 16. 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
  17. 17. 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];
  18. 18. • • •
  19. 19. LAG (scalar_expression [,offset] [,default]) OVER ([partition_by_clause] order_by_clause) • scalar_expression – • Offset – • Default – • OVER –
  20. 20. • – – • – –
  21. 21. CUME_DIST ( ) OVER ([ partition_by_clause ] order_by_clause) • • •
  22. 22. PERCENT_RANK ( ) OVER ([ partition_by_clause ] order_by_clause) • • •
  23. 23. • •
  24. 24. PERCENTILE_DISC ( numeric_literal) WITHIN GROUP ( ORDER BY expression) OVER ([<partition_by_clause> ] ) •
  25. 25. PERCENTILE_CONT ( numeric_literal) WITHIN GROUP ( ORDER BY expression) OVER ([<partition_by_clause> ] ) • •
  26. 26. • •
  27. 27. Deck by Shy Engelberg and Kevin Boles

×