T-SQL Power! The OVER Clause: Your Key to No-Sweat Problem Solving

Slides:



Advertisements
Similar presentations
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Advertisements

© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
MIX 09 4/15/ :14 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Session 1.
Built by Developers for Developers…. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.

customer.
demo © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
demo Demo.
demo QueryForeign KeyInstance /sm:body()/x:Order/x:Delivery/y:TrackingId1Z
projekt202 © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks.
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.

How We Do Language Design at Microsoft (C#, Visual Basic, F#)
Tech·Ed  North America /11/ :01 AM SESSION CODE: DEV405
6/12/ :53 PM DEV311 Deep Dive into Microsoft Visual Studio Team Foundation Server 2010 Reporting Steven Borg, Principal ALM Consultant Northwest.
9/11/2018 5:53 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
MDOP: Advanced Group Policy Management 4.0
Tech Ed North America /13/ :13 AM Required Slide
Возможности Excel 2010, о которых следует знать
Overview of Social Computing in Microsoft SharePoint 2010
Customer Experiences With Business Intelligence
Implementing RESTful Services Using the Microsoft .NET Framework
Sysinternals Tutorials
Title of Presentation 11/22/2018 3:34 PM
What’s New In ASP.NET MVC 2
Advanced Dashboard Creation Using Microsoft SharePoint Server 2010
Jason Zander Unplugged
Branching and Merging Practices
Title of Presentation 12/2/2018 3:48 PM
High Availability: A Contrarian View
Business Intelligence Overview: Decisions, Decisions, Decisions
Better Together: Secure SQL Server on Secure Windows
12/5/2018 3:24 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Windows 7 Deployment en Masse
Best Practices: Creating OData Services using WCF Data Services
Team Foundation Server 2010 for Everyone
Authoring for Microsoft Silverlight 4 with Microsoft Expression Blend
Tech Ed North America /1/ :36 AM Required Slide
Tech Ed North America /1/2019 2:58 AM Required Slide
Microsoft SharePoint Conference 2009 Jon Flanders
Tech·Ed North America /22/2019 7:40 PM
Building Silverlight Apps with RIA Services
Tech Ed North America /12/2019 6:45 AM Required Slide
Silverlight Debugging
8/04/2019 9:13 PM © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
A Lap Around Internet Explorer 9 For Developers
Виктор Хаджийски Катедра “Металургия на желязото и металолеене”
2010 Microsoft BI Conference
Hack-proofing your Clients using Windows 7 Security!
PENSACOLA ENERGY WORK PLAN OCTOBER 10, 2016
Title of Presentation 5/12/ :53 PM
Шитманов Дархан Қаражанұлы Тарих пәнінің
Lap Around the Windows Azure Platform
Code First Development in Microsoft ADO.NET Entity Framework 4.1
Building BI applications using PowerPivot for Excel
Title of Presentation 5/24/2019 1:26 PM
7/5/2019 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Tech·Ed North America /6/2019 7:10 PM
日本初公開!? Vista の新機能を実演 とっちゃん わんくま同盟 7/23/2019 9:09 AM
Title of Presentation 7/24/2019 8:53 PM
TechEd /30/2019 7:18 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Tech Ed North America /6/2019 2:07 PM Required Slide
Tech·Ed North America /7/2019 2:30 PM
What’s New in Visual Studio 2012 for Web Developers
Presentation transcript:

T-SQL Power! The OVER Clause: Your Key to No-Sweat Problem Solving Tech Ed North America 2010 11/18/2018 2:11 PM Required Slide SESSION CODE: DAT317 T-SQL Power! The OVER Clause: Your Key to No-Sweat Problem Solving Adam Machanic Database Consultant SQLblog © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

WHAT IS POWER?

INNOVATION SHOULD BE EASY!

About Me amachanic@gmail.com Independent SQL Server and .NET Consultant Boston, MA Author SQL Server 2008 Internals Expert SQL Server 2005 Development Conference and INETA Speaker Connections, PASS, TechEd, DevTeach, etc. Co-Founder: SQLblog.com The SQL Server Blog Spot on the Web amachanic@gmail.com

Grouping vs. Partitioning Grouping is an N1 transformation { ColA ColB Val A B 1 2 3 C D 4 5 3 rows  1 row { 2 rows  1 row

Grouping vs. Partitioning Grouping is an N1 transformation ColA ColB SUM(Val) A B 6 C D 9 { 1 row { 1 row We gain insight, but lose information

Grouping vs. Partitioning Partitioning is an NN transformation { ColA ColB Val A B 1 2 3 C D 4 5 3 rows  3 rows { 2 rows  2 rows

Grouping vs. Partitioning Partitioning is an NN transformation { ColA ColB Val SUM(Val) OVER (…) A B 1 6 2 3 C D 4 9 5 3 rows { 2 rows We gain insight, and lose nothing

Introducing the OVER Clause First available in SQL Server 2005 Can be applied to any system or user-defined aggregate for partitioning Can be applied to a new set of “windowed” functions for ordered metrics

Aggregation: The Old Way Tech Ed North America 2010 11/18/2018 2:11 PM Aggregation: The Old Way SELECT ColA, ColB, SUM(Val) FROM T1 GROUP BY ColB © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Aggregation: With OVER SELECT ColA, ColB, SUM(Val) OVER ( PARTITION BY ColB ) FROM T1

We Aren’t Required to Partition… SELECT ColA, ColB, SUM(Val) OVER() FROM T1

Multiple Partitions in One Query? Sure. SELECT ColA, ColB, SUM(Val) OVER ( PARTITION BY ColB ) AS PartitionedVal, SUM(Val) OVER() AS NonPartitionedVal FROM T1

Ranking and Numbering Functions SQL Server 2005/2008 windowing functions: ROW_NUMBER RANK DENSE_RANK NTILE These functions can be both partitioned and ordered

ROW_NUMBER Uniquely numbers rows Tech Ed North America 2010 11/18/2018 2:11 PM ROW_NUMBER Uniquely numbers rows ColA ColB ROW_NUMBER() OVER (ORDER BY ColA) A Z 1 B Y 2 C X 3 D W 4 E V 5 © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Tech Ed North America 2010 11/18/2018 2:11 PM ROW_NUMBER – Partitioned Uniquely numbers rows, restarting at each partition ColA ColB ROW_NUMBER() OVER (PARTITION BY ColA ORDER BY ColB) A X 1 Y 2 Z 3 B © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

RANK Ranks rows, skipping numbers for ties Tech Ed North America 2010 11/18/2018 2:11 PM RANK Ranks rows, skipping numbers for ties ColA ColB RANK() OVER (ORDER BY ColA) A Z 1 Y B X 3 W C V 5 © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

DENSE_RANK Ranks rows, not skipping numbers for ties Tech Ed North America 2010 11/18/2018 2:11 PM DENSE_RANK Ranks rows, not skipping numbers for ties ColA ColB DENSE_RANK() OVER (ORDER BY ColA) A Z 1 Y B X 2 W C V 3 © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

NTILE(x) Creates closest-match subgroups based on desired size “x” Tech Ed North America 2010 11/18/2018 2:11 PM NTILE(x) Creates closest-match subgroups based on desired size “x” ColA ColB NTILE(3) OVER (ORDER BY ColA) A Z 1 Y B X W 2 C V © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Under the Covers You didn’t think this was all free—right? Partitioning and ordering both require input rows to be sorted Indexes can be aligned to reduce or eliminate the need to sort All of the partitioning work is done with the Segment iterator Watch out for spools!

And Now: A Bunch of Demos!

The OVER Clause, In Depth! demo

Summary The OVER clause makes powerful T-SQL easy… …and perhaps even fun Great performance if you do a bit of work upfront Don’t forget to be creative!

Required Slide Track PMs will supply the content for this slide, which will be inserted during the final scrub. Tech Ed North America 2010 11/18/2018 2:11 PM DAT Track Scratch 2 Win Find the DAT Track Surface Table in the Yellow Section of the TLC Try your luck to win a Zune HD Simply scratch the game pieces on the DAT Track Surface Table and Match 3 Zune HDs to win © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Resources Learning Required Slide www.microsoft.com/teched Tech Ed North America 2010 11/18/2018 2:11 PM Required Slide Resources Learning Sessions On-Demand & Community Microsoft Certification & Training Resources www.microsoft.com/teched www.microsoft.com/learning Resources for IT Professionals Resources for Developers http://microsoft.com/technet http://microsoft.com/msdn © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Complete an evaluation on CommNet and enter to win! Tech Ed North America 2010 11/18/2018 2:11 PM Required Slide Complete an evaluation on CommNet and enter to win! © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31st http://northamerica.msteched.com/registration   You can also register at the North America 2011 kiosk located at registration Join us in Atlanta next year

Tech Ed North America 2010 11/18/2018 2:11 PM © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Required Slide Tech Ed North America 2010 11/18/2018 2:11 PM © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.