Mastering T-SQL Window Functions

  • Published on
    11-Jul-2015

  • View
    218

  • Download
    3

Embed Size (px)

Transcript

Title of the presentation

Mastering T-SQL Window FunctionsJose L. RiveraMVP | MCITP | MCTS | MCSAChapter Leader | PASS Global Spanish VC9/28/20141Microsoft SharePoint 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.AboutJose L. Rivera is a seasoned data professional, speaker, SQL Server MVP and community leader working as Consultant for Nagnoi LLC. He shares his knowledge and experience on his blog (sqlconqueror.com) and throughout his community work as Chapter Leader for the PASS Global Spanish VC (facebook.com/SpanishPASSVC) and board member of the Puerto Rico PASS (prpass.org). Jose L is a frequent speaker in SQL Saturday events throughout US and Latin America.Twitter @SQLConquerorLinkedIn /in/SQLConquerorFaceBook /SQLConquerorBlog http://sqlconqueror.comJos L. RiveraChapter LeaderPASS Global Spanish VC

9/28/20142Microsoft SharePoint 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.AgendaWindows Functions OverviewOVER ClauseFunctions CategoriesRankingAggregationAnalytic

Q&A

3Windows Functions OverviewIntroduced in SQL Server 2005, enhanced in SQL Server 2012a function applied to a set of rowsthe window specification is implemented using the OVER clause

Still a work in progress!The OVER clauseDefines the set (window) over which the function will be appliedhttp://msdn.microsoft.com/en-us/library/ms189461(v=sql.110).aspxOVER ( )Divide the query result set into partitions and the operation is applied to each partition separatelyDefines the logical order of the rows within each partition of the result setLimits the rows within the partition by specifying start and end points within the partitionSet-Based operations (Windowing)IDAcctIDTransDateTransAmt1123427/11/2012$150.002123427/11/2012$22.003567828/11/2012$50.004567828/11/2012$150.005567828/11/2012$10.006567829/11/2012$120.007098730/11/2012$20.008098730/11/2012$100.009098730/11/2012$50.00Aggregation Window:SUM(TransAmt) OVER(PARTITION BY TransDate)Ranking Window:ROW_NUMBER() OVER(PARTITION BY TransDate ORDER BY AcctID, ID)AcctIDTransDateTransAmtBalAmt567828/11/2012$50.00$50.00567828/11/2012$150.00$200.00567828/11/2012$10.00$210.00AcctIDTransDateTransAmtBalAmtRank567828/11/2012$50.00$50.001567828/11/2012$150.00$200.002567828/11/2012$10.00$210.003AcctIDTransDateTransAmt567828/11/2012$50.00567828/11/2012$150.00567828/11/2012$10.00ROW or RANGE (window frame)Defines how you move within the window to define rows selection

BETWEEN AND

PRECEDING FOLLOWINGWindows Functions CategoriesRankingAggregationAnalyticROW_NUMBER()RANK()DENSE_RANK()NTILE()SUM() | AVG() | COUNT()MIN() | MAX()CHECKSUM_AGGSTDEV() | STDEVP()VAR() | VARP()LEAD() | LAG()FIRST_VALUE() | LAST_VALUE()CUME_DIST()PERCENT_RANK()PERCENTILE_DIST()PERCENTILE_CONT()Ranking FunctionsROW_NUMBER()Generates a sequential number (starts at 1)RANK()Ranks the data (Olympic rank in case of a tie)DENSE_RANK()Ranks the data without gapsNTILE()Distributes the data into groups/bucketsNo Framing AvailableRanking FunctionsDEMOAggregation FunctionsSUM() | AVG() | COUNT() | COUNT_BIG() | MIN() | MAX()Commonly used, no surprise here

CHECKSUM_AGGReturns the checksum of the values in a groupSTDEV() | STDEVP()statistical standard deviation (regular or population)VAR() | VAR()statistical variance (regular or population)

Aggregation FunctionsDEMOAnalytic Functions

LEAD() | LAG()Accesses data from a subsequent/previous row in the same result set No Framing AvailableLEAD | LAG (scalar_expression [,offset] [,default]) OVER ( [ partition_by_clause ] order_by_clause )Analytic Functions

FIRST_VALUE() | LAST_VALUE()Returns the first/last value in an ordered set of valuesFIRST_VALUE | LAST_VALUE( [scalar_expression] ) OVER ( [ partition_by_clause ] order_by_clause rows_range_clause )Analytic Functions

CUME_DIST()Calculates the cumulative distribution of a value in a group of valuesNo Framing AvailableCUME_DIST()OVER ( [ partition_by_clause ] order_by_clause )Analytic Functions

PERCENT_RANK()Calculates the relative rank of a row within a group of rowsNo Framing AvailablePERCENT_RANK()OVER ( [ partition_by_clause ] order_by_clause )Analytic Functions

PERCENTILE_DIST()Computes a specific percentile for sorted values in an entire rowset or within distinct partitions of a rowsetNo Framing AvailablePERCENTILE_DIST ( numeric_literal )WITHIN GROUP ( ORDER BY order_by_expression [ ASC | DESC ] ) OVER ( [ partition_by_clause ] order_by_clause rows_range_clause )Analytic Functions

PERCENTILE_CONT()Calculates a percentile based on a continuous distribution of the column value. The result is interpolated and might not be equal to any of the specific values in the columnNo Framing AvailablePERCENTILE_CONT ( numeric_literal )WITHIN GROUP ( ORDER BY order_by_expression [ ASC | DESC ] ) OVER ( [ partition_by_clause ] order_by_clause rows_range_clause )Analytic FunctionsDEMO

jose@sqlconqueror.com

Recommended

View more >