Sql server windowing functions

1,849 views

Published on

Session teached for the Himalayan SQL Server User Group about windowing functions in SQL Server 2012

Published in: Technology
  • Be the first to comment

Sql server windowing functions

  1. 1. SQL Server WindowingFunctionsEnrique Catalá BañulsMentor, SolidQMAP 2012 – Microsoft Technical Ranger – Microsoft Certified Trainerecatala@solidq.comTwitter: @enriquecatala© 2012 SolidQ
  2. 2. Enrique Catalá Bañuls•Computer engineer•Mentor at SolidQ in the relational engine team•Microsoft Technical Ranger•Microsoft Active Professional•Microsoft Certified Trainer© 2012 SolidQecatala@solidq.comTwitter: @enriquecatala
  3. 3. 3© 2012 SolidQAbout SolidQ…• SolidQ sets the standard for information management services by providing the worldsmost trusted and reliable business intelligence, data management, collaboration and customsolutions for Microsofts cloud and on-premise platforms. SolidQ is recognized as an expertamong its peers:• 80 of the world’s top information experts in our BI and data managementteam, including more than 30 SQL MVPs• Authored over 30 industry leading books• Frequent speakers at local and international SQL Server technicalconferences such as TechEd around the world, the BI Conference, SQL PASSSummit, and SQL Saturdays• Serving over 800 clients in 22 countries• We are so confident in the capabilities of our team and the value weprovide, we unconditionally guarantee your satisfaction with our services.• For more information visit www.solidq.com
  4. 4. SolidQTM Journal• Free, monthly e-magazine providing answers you need to be successful withyour Microsoft SQL Server, Business Intelligence, and software developmentsolutions• Featuring in-depth technical articles, real-life case studies, practical columns,and seasoned perspectives from our lineup of influential global experts—both inside SolidQ and around the community• Helping busy IT pros make the best decisions about which technologies touse when and where, how to get the most out of their systems, and how tobe more productive and effective in their organizations• Read today at www.solidq.com/sqjReal Practices forthe Real World© 2012 SolidQ – Think fast, move fast 4
  5. 5. Objectives of this session•Introduction•The past, the present and the future•Window Functions• Why do we need window functions?• Syntax• Performance• Why almost complete?•Q&A5 |© 2012 SolidQ
  6. 6. Window Functions•Window function• function applied to a set of rows defined by a windowdescriptor and returns a single value for each rowfrom the underlying query•Window descriptor• Define which rows will be used with the function© 2012 SolidQ
  7. 7. Objectives of this session•Introduction•The past, the present and the future•Window Functions• Why do we need window functions?• Syntax• Performance• Why almost complete?•Q&A7 |© 2012 SolidQ
  8. 8. The past, the present and the future•The SQL 2000 way• SQL Server 2000 does not provide any syntax tosupport windowing functions•The SQL 2005 way• SQL Server 2005 to 2008R2 introduced a partialimplementation of the windowing functions using theOVER clause•The SQL 2012 way• SQL Server 2012 will have an almost completeimplementation of the windowing functions© 2012 SolidQ
  9. 9. The SQL 2005-2008R2 way•SQL Server 2005 introduces• OVER clause (partially implemented)• New window functionso ROW_NUMBER()o RANK()o DENSE_RANK()o NTILE()•SQL Server 2008/2008R2 added no newimplementations in this particular area© 2012 SolidQ
  10. 10. The SQL 2005-2008R2 way•OVER clause•Two different implementations depending ontype of windowing• Aggregate window functions does not provide theORDER BY clause© 2012 SolidQ
  11. 11. The SQL 2012 way• SQL Server 2012 are getting close to the fullimplementation of Windowing functions• SQL Server 2012 introduces:• OVER clause almost completeo Order byo Window Frame• 8 new window functionso LAG(), LEAD()o FIRST_VALUE(), LAST_VALUE()o CUME_DIST()o PERCENT_RANK()o PERCENTILE_DISC(), PERCENTILE_COUNT()© 2012 SolidQ
  12. 12. OVER clause syntax•SQL Server 2005/2008/R2• SQL Server 2012© 2012 SolidQ
  13. 13. Objectives of this session•Introduction•The past, the present and the future•Window Functions• Why do we need window functions?• Syntax• Performance• Why almost complete?•Q&A13 |© 2012 SolidQ
  14. 14. Why do we need windows functions?• Why do we need windows functions?• What if we want to get the sum and the valuecolumn? (group and detail)• If we are in SQL 2005/2008/2008R2…am i correct withthis approach?select id_table, value,sum(value) as [sum(value)]from table1 group by id_tableMsg 8120, Level 16, State 1, Line 1Column table1.value is invalid in the select list becauseit is not contained in either an aggregate function or theGROUP BY clause.id_table value1 12 12 23 13 23 3SUM(value)136id_table value sum(value)1 1 12 1 32 2 33 1 63 2 63 3 6Select sum(value) as [sum(value)]from table1 group by id_table© 2012 SolidQ
  15. 15. Why do we need windows functions?•Now we know that the OVER clause exists…•This must be our solutionselect id_table,value,sum(value) over(partition by id_table)from table1id_table value sum(value)1 1 12 1 32 2 33 1 63 2 63 3 6© 2012 SolidQ
  16. 16. New syntax to solve oldproblems• Partitioning• Ordering• Slicing/framing© 2012 SolidQ
  17. 17. Key concepts: All togetherPartitionUNBOUNDEDFOLLOWINGUNBOUNDEDPRECEDINGCURRENTROW© 2012 SolidQ
  18. 18. Key concepts© 2012 SolidQ
  19. 19. Key concepts: Partition•Partition is like a group ofrows with similar“characteristics” inside a set© 2012 SolidQ
  20. 20. Key concepts: Partition•Partition is like a group ofrows with similar“characteristics” inside a set© 2012 SolidQ
  21. 21. Key concepts: Slicing/Framing• RANGE/ROWS• ROWS | RANGE BETWEEN <B1> AND <B2>• ROWS | RANGE <B1>© 2012 SolidQ
  22. 22. Key concepts: Slicing/Framing• B1 and B2 can be• UNBOUNDED PRECEDING• UNBOUNDED FOLLOWING• CURRENT ROW• For “ROWS” clause onlyo <scalar expression> PRECEDINGo <sclara expression> FOLLOWING• Note• B1 <= B2 or NULL will be returnedo Except in COUNT() that 0 will be returned© 2012 SolidQ
  23. 23. Key concepts: All togetherPartitionUNBOUNDEDFOLLOWINGUNBOUNDEDPRECEDINGCURRENTROW© 2012 SolidQ
  24. 24. New windowing functions• New analytic functions in SQL 2012• offset• LAG()• LEAD()• FIRST_VALUE()• LAST_VALUE()• Distribution• PERCENT_RANK()• CUME_DIST()• PERCENTILE_CONT()• PERCENTILE_DIST()© 2012 SolidQ
  25. 25. DEMO 1: NEW FUNCTIONSAND SYNTAXBasics on framing, RANGE vs ROWS, new window functions© 2012 SolidQ
  26. 26. Objectives of this session•Introduction•The past, the present and the future•Window Functions• Why do we need window functions?• Syntax• Performance• Why almost complete?•Q&A26 |© 2012 SolidQ
  27. 27. Objectives of this session•Introduction•The past, the present and the future•Window Functions• Why do we need window functions?• Syntax• Performance• Why almost complete?•Q&A27 |© 2012 SolidQ
  28. 28. Performance: IN-MEMORY vs ONDISK• Directly affected by Window Spool Operator• Used to store the framing data• In-memory vs disk-based worktable• In-memory worktable• Fastest• Prerequisites: Framing defined using ROWS and workingwith <10k rows• Disk-based worktable• Default• Used when the window was defined using RANGE• Used when the windows was defined using ROWS and theframe has more than 10k rows© 2012 SolidQ
  29. 29. Performance: Indexing•Index order by the window partitioningelements and then by the order elements•Include the rest of the columns•Optimized execution operators:1. Scan2. Segment3. Window spool4. Aggregate© 2012 SolidQ© 2012 SolidQ
  30. 30. DEMO 2: PERFORMANCERunning Totals, in memory vs on disk, …© 2012 SolidQ
  31. 31. Why almost complete?• RANGE only supports• UNBOUNDED PRECEDING• UNBOUNDED FOLLOWING• CURRENT ROW• Ranking functions does not support framing:• ROW_NUMBER, RANK, NTILE,DENSE_RANK,PERCENTILE_DIST, PERCENTILE_CONT• Not implemented• WINDOW aliases• Clauses: NULLs FIRST and NULLs LAST© 2012 SolidQ
  32. 32. Objectives of this session•Introduction•The past, the present and the future•Window Functions• Why do we need window functions?• Syntax• Performance• Why almost complete?•Q&A32 |
  33. 33. THANK YOU!Enrique Catalá BañulsMentor, SolidQMAP 2012 – Microsoft Technical Ranger – Microsoft Certified Trainerecatala@solidq.comTwitter: @enriquecatala

×