DBI309: Using SQL Server 2012 Window Functions to Solve Common T-SQL Challenges Steven Wang MCITP – BI, Database Developer and DBA
Senior DBA Write MSBICOE.com Active Participant One of Top Answers Steven Wang
Agenda 2. ********** (Encrypted) 1. Solution Comparison 3.The Nitty- Gritties 5. Performance Comparison Demo
Note: The table is simplified for demo Purpose.
Note: The table is simplified for demo Purpose. A. 2000B. 2005C D E. Wrong Syntax
A. 2000B. 2005C D E. Wrong Syntax
A. 2000B. 2005C D E. Wrong Syntax
A. B.
--Quoted from PostgreSQL Documentation --SQL Server 2012, BOL
SQL: 2008 SQL: 2003 SQL: 2011 SQL: 2006 ISO/ANSI Standard Window Functions introduced No Changes on Window Functions Window Functions Enhanced Extensive Coverage on Window Functions Over Clause (Limited) Rank Functions SQL Server 2005 No new Features for Window Functions SQL Server 2008 Extended support on Over Clause Distribution Functions Offset Functions SQL Server 2012 SQL Server VNext
Over()Over( ) Sum(Amount) Over( ) Partition By EmployeeID Order By Period Desc Rows Between Unbounded Preceding And 2 Following
Sum(Amount) Over( ) Partition By EmployeeID Order By Period Rows Between Unbounded Preceding And 2 Following Partition By Order by Frame 1 Frame 2 Frame 3 Frame 4 Frame 5 Frame 6
FromWhereGroup ByHavingSelectOrder By fwgh SO
Agg([All] Expression) Over(…) Agg(Exp) Over(…) Agg(All Exp) Over(…) Agg(Distinct Exp) Over(…) No Distinct Aggregations with Over() Note: Agg refers to all aggregation functions
fwgh SO No Distinct in aggregation with Over() A few more
A. 1 B C. Both valid D. Both not
demo The T-SQL Challenge Solutions
Subquery Window Function
demo Performance Comparisons
Running Total Benchmark
SELECT Reverse( Concat(Col, Lead(Col,1) Over(Order by Col Desc) ) ) As Conclusion FROM ( Values ('!!lacirogamsatnahp'), (' era snoitcnuF wodniW') ) T(Col) ORDER BY Col Desc OFFSET 0 ROW FETCH FIRST 1 ROW ONLY;
DBI305: Practical Uses and Optimisation of New T-SQL Features in Microsoft SQL Server Dave Dustin Exam : Querying Microsoft SQL Server 2012 Contact Me Later at LinkedIn Search: Steven Wang Blog: Required Slide *delete this box when your slide is finalised Speakers, please list the Breakout Sessions and Certification Exams that relate to your session. Make sure to indicate how delegates may contact you, if they have questions after the session.
Win instant prizes by evaluating this session: aka.ms/mobile
Connect. Share. Discuss. newzealand.msteched.com Learning Microsoft Certification & Training Resources microsoft.com/learning TechNet Resources for IT Professionals microsoft.co.nz/technet Resources for Developers microsoft.co.nz/msdn