Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Window functions with SQL Server 2016

2,269 views

Published on

Required for certification, window functions are powerful analytic functions built into SQL Server. SQL Server 2005 introduced the core window ranking functions, and SQL Server 2012 added time and statistical percentage window functions. These functions allow for advanced variable creation, and are of direct benefit to people creating features for data science. This talk will also recommend further reading on this topic.

Published in: Data & Analytics
  • Be the first to comment

Window functions with SQL Server 2016

  1. 1. Window Functions with SQL Server 2016 MARK TABLADILLO SENIOR DATA SCIENTIST, PREDICTIX/LOGICBLOX SQL SATURDAY 439 COLUMBUS, GA – OCTOBER 2015
  2. 2. Abstract Window functions are powerful analytic functions built into SQL Server. SQL Server 2005 introduced the core window ranking functions, and SQL Server 2012 added time and statistical percentage window functions. These functions allow for advanced variable creation, and are of direct benefit to people creating features for data science. This talk will also recommend further reading on this topic. Required for Certification
  3. 3. What is a “Window”? Answer: A set of rows defined by the OVER clause Set One Set Two Set Three ORDER BY Set One Set Two Set Three PARTITION BY
  4. 4. What is a “function”? Function Example Ranking ROW_NUMBER, RANK, DENSE_RANK, NTILE Aggregate MIN, MAX, AVG, SUM, COUNT, STDEV, STDEVP, VAR, VARP CHECKSUM_AGG, COUNT_BIG Analytic LAG, LEAD, FIRST_VALUE, LAST_VALUE, PERCENT_RANK, PERCENTILE_CONT, PERCENTILE_DISC, CUME_DIST GROUP BY not required
  5. 5. Demo: Ranking
  6. 6. What is a “Window”? Answer: A set of rows defined by the OVER clause Set One Set Two Set Three ORDER BY Set One Set Two Set Three PARTITION BY
  7. 7. What is a “function”? Function Example Ranking ROW_NUMBER, RANK, DENSE_RANK, NTILE Aggregate MIN, MAX, AVG, SUM, COUNT, STDEV, STDEVP, VAR, VARP Analytic LAG, LEAD, FIRST_VALUE, LAST_VALUE, PERCENT_RANK, PERCENTILE_CONT, PERCENTILE_DISC, CUME_DIST GROUP BY not required
  8. 8. Order please
  9. 9. Demo: Aggregate
  10. 10. What is a “Window”? Answer: A set of rows defined by the OVER clause Set One Set Two Set Three ORDER BY Set One Set Two Set Three PARTITION BY
  11. 11. What is a “function”? Function Example Ranking ROW_NUMBER, RANK, DENSE_RANK, NTILE Aggregate MIN, MAX, AVG, SUM, COUNT, STDEV, STDEVP, VAR, VARP Analytic LAG, LEAD, FIRST_VALUE, LAST_VALUE, PERCENT_RANK, PERCENTILE_CONT, PERCENTILE_DISC, CUME_DIST GROUP BY not required
  12. 12. Framing: Order Matters Term Operators ROWS = Physical Operator (faster) UNBOUNDED PRECEDING UNBOUNDED FOLLOWING N PRECEDING N FOLLOWING CURRENT ROW RANGE = Logical Operator (slower) UNBOUNDED PRECEDING UNBOUNDED FOLLOWING CURRENT ROW (RANGE?)
  13. 13. Default Frame First row of partition to current row
  14. 14. 1 2 3 4 5 6 7 8 9 Current Row Unbounded FollowingUnbounded Preceding 4 Preceding 2 Following 1 Preceding 1 Following ROW
  15. 15. Current Row Unbounded Following Unbounded Preceding RANGE 1 2 3 4 5 6 7 8 9 12 23 34 45 50 50 50 50 65
  16. 16. Demo: Framing
  17. 17. What is a “Window”? Answer: A set of rows defined by the OVER clause Set One Set Two Set Three ORDER BY Set One Set Two Set Three PARTITION BY
  18. 18. What is a “function”? Function Example Ranking ROW_NUMBER, RANK, DENSE_RANK, NTILE Aggregate MIN, MAX, AVG, SUM, COUNT, STDEV, STDEVP, VAR, VARP Analytic LAG, LEAD, FIRST_VALUE, LAST_VALUE, PERCENT_RANK, PERCENTILE_CONT, PERCENTILE_DISC, CUME_DIST GROUP BY not required
  19. 19. Demo: Analytic
  20. 20. Caveats Exist But if Window Functions alone cannot do the job, Then something else can
  21. 21. Logical Alternatives  Common Table Expressions (CTEs)  CROSS APPLY
  22. 22. Nondeterministic Functions = Not one unique way to do the job https://msdn.microsoft.com/en- us/library/ms178091.aspx

×