SlideShare une entreprise Scribd logo
1  sur  26
A Window into Your Data
Using SQL Window Functions

Steve Hughes
Practice Lead, Data and BI
Agenda
» SQL Window Functions Overview
» SQL Server 2008 Functions
» SQL Server 2012 Functions
» Wrap Up
What Are SQL Window Functions &
Why Should We Care?
» Introduced in 2005, expanded in 2012
» Used for grouping data for use at the row level
» Allow for solving complex business questions
  » % of customer’s total sales
  » Running total
  » Next and previous row values
» Does not require GROUP BY or subselect queries
Structure of the OVER Clause
» OVER – used to define the window over which the function will be
  applied. The default window is the entire set.
  » PARTITION BY – groups the data by one or more columns
  » ORDER BY – sorts the data in the partition
  » ROWS or RANGE – further reduces the size of the window based on
    proximity from the current row

» OVER([PARTITION BY <<field list>>]
  [ORDER BY <<field list>>]
  [ROWS/RANGE <<set of rows>> ])
SQL 2008 Functionality
» Ranking window functions
OVER([PARTITION BY value_expression, … [n]] <ORDER
BY_Clause>)
   » ROW_NUMBER
   » DENSE_RANK
   » RANK
   » NTILE
» Aggregate window functions
OVER([PARTITION BY value_expression, … [n]])
   » Most aggregate function, e.g. SUM, COUNT, AVG
     » Does not support GROUPING or GROUPING_BY aggregations
  » Does not support ORDER BY
SQL 2008 Window Illustrated

 OrderID   OrderDate   OrderAmt     Aggregation Window – DailyTotal:
                                    SUM(OrderAmt) OVER (PARTITION BY OrderDate)
 1         3/1/2012        $10.00
 2         3/1/2012        $11.00                    OrderID   DailyTotal   DailyRank




                                      PARTITION BY
                                       OrderDate
 3         3/2/2012        $10.00
 4         3/2/2012        $15.00                    3         $42.00       3
 5         3/2/2012        $17.00                    4         $42.00       2
 6         3/3/2012        $12.00                    5         $42.00       1
 7         3/4/2012        $10.00
 8         3/4/2012        $18.00       Ranking Window – DailyRank:
 9         3/4/2012        $12.00       RANK() OVER (PARTITION BY OrderDate
                                        ORDER BY OrderAmt DESC)
SQL 2008 SQL Comparison:
SubSelect vs Window Function
select
(select sum(OrderAmt) from CTEOrders o where o.OrderDate
= CTEOrders.OrderDate)
as DailyOrderAmt
from CTEOrders

select SUM(OrderAmt) OVER (PARTITION BY OrderDate)
from CTEOrders
order by OrderID
Why Not Group By?
select OrderDate ,sum(OrderAmt) as DailyOrderAmt
from CTEOrders
group by OrderDate
» This returns the amount and is the subselect logic

select OrderDate
        ,OrderID
        ,OrderAmt
        ,sum(OrderAmt) as DailyOrderAmt
from CTEOrders
group by OrderDate
        ,OrderID
        ,OrderAmt

»   This returns the details, but the sum is not the correct
One Twist on Group By
select sum(OrderAmt)
, sum(OrderAmt) over() as TotalOrderAmt
from CTEOrders
group by CustomerName
» This returns an error because the first expression is an aggregate, but not the second
» Also, OVER() is evaluated over the full set

select sum(OrderAmt)
, sum(sum(OrderAmt)) over() as TotalOrderAmt
from CTEOrders
group by CustomerName

»   This returns the aggregate of the aggregates and does not return an error
»   Thanks to Itzik Ben-Gan for this tip
SQL 2008 Demos
» Group By and SubSelect
» Ranking Windows
» Aggregate Windows
» Twisted Group By
SQL 2012 Functionality
» Significantly expanded in this release
  » Greater ANSI compliance
  » Matches Oracle 11g
» ORDER BY clause supported by all function types, not just ranking functions
» ROWS and RANGE clauses added to OVER clause
  » This allows for running totals and other hard to resolve problems
» New analytic functions including:
  » Position values (e.g. LAG, LAST_VALUE)
  » Percentile values and rank (e.g. PCT_RANK, PERCENTILE_DISC)
SQL 2012 Window Illustrated (1)
2008 Functionality

  OrderID   OrderDate   OrderAmt     Aggregation Window – DailyTotal:
                                     SUM(OrderAmt) OVER (PARTITION BY OrderDate)
  1         3/1/2012        $10.00
  2         3/1/2012        $11.00                    OrderID   DailyTotal   DailyRank




                                       PARTITION BY
                                        OrderDate
  3         3/2/2012        $10.00
  4         3/2/2012        $15.00                    3         $42.00       3
  5         3/2/2012        $17.00                    4         $42.00       2
  6         3/3/2012        $12.00                    5         $42.00       1
  7         3/4/2012        $10.00
  8         3/4/2012        $18.00      Ranking Window – DailyRank:
  9         3/4/2012        $12.00      RANK() OVER (PARTITION BY OrderDate
                                        ORDER BY OrderAmt DESC)
SQL 2012 Window Illustrated (2)
Aggregate with Order By Functionality                           Aggregate Window –
                                                                Running Total by Customer:
                                                                SUM(OrderAmt) OVER
  OrdID   OrdDt      OrdAmt     Cust                            (PARTITION BY CustomerName
                                                                ORDER BY OrderDate, OrderID)
  1       3/1/2012     $10.00      Joe
  2       3/1/2012     $11.00      Sam




                                         PARTITION BY Cust
                                                             Cust   OrdDt      OrdID   RunTot
  3       3/2/2012     $10.00     Beth
  4       3/2/2012     $15.00      Joe                       Sam    3/1/2012   2       $11.00
  5       3/2/2012     $17.00      Sam
                                                             Sam    3/2/2012   5       $28.00
  6       3/3/2012     $12.00      Joe
  7       3/4/2012     $10.00     Beth
                                                             Sam    3/4/2012   8       $46.00
  8       3/4/2012     $18.00      Sam
  9       3/4/2012     $12.00      Joe
SQL 2012 Window Illustrated (2)
Aggregate with Order By Functionality

 select OrderID
 ,OrderDate
 ,OrderAmt
 ,CustomerName
 ,SUM(OrderAmt)
    OVER (PARTITION BY CustomerName
          ORDER BY OrderDate, OrderID)
 as RunningByCustomer
 from CTEOrders
SQL 2012 Window Illustrated (3) Aggregate Rows Window –
Aggregate with Rows Functionality                               Next 2 Orders by Customer:
                                                                SUM(OrderAmt) OVER
                                                                (PARTITION BY CustomerName
                                                                ORDER BY OrderDate, OrderID ROWS
  OrdID   OrdDt      OrdAmt     Cust                            BETWEEN 1 FOLLOWING AND 2
                                                                FOLLOWING)
  1       3/1/2012     $10.00      Joe
  2       3/1/2012     $11.00      Sam
                                                             Cust   OrdDt      OrdID   NextTwo




                                         PARTITION BY Cust
  3       3/2/2012     $10.00     Beth
                                                             Joe    3/1/2012   1       $27.00
  4       3/2/2012     $15.00      Joe
  5       3/2/2012     $17.00      Sam                       Joe    3/2/2012   4       $24.00
  6       3/3/2012     $12.00      Joe
                                                             Joe    3/3/2012   6       $12.00
  7       3/4/2012     $10.00     Beth
                                                             Joe    3/4/2012   9       NULL
  8       3/4/2012     $18.00      Sam
  9       3/4/2012     $12.00      Joe
SQL 2012 Window Illustrated (3)
Aggregate with Rows Functionality
  select OrderID
  ,OrderDate
  ,OrderAmt
  ,CustomerName
  ,SUM(OrderAmt)
     OVER (PARTITION BY CustomerName
           ORDER BY OrderDate, OrderID
           ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING)
  as NextTwoAmts
  from CTEOrders
  ROWS clause sets the window based on physical proximity to current row.
SQL 2012 Window Illustrated (4)                                 Aggregate Range Window –
                                                                Running Total by Customer:
Aggregate with Range Functionality                              SUM(OrderAmt)
                                                                OVER (PARTITION BY CustomerName
                                                                ORDER BY OrderDate, OrderID
  OrdID   OrdDt      OrdAmt     Cust                            RANGE BETWEEN UNBOUNDED
                                                                PRECEDING and CURRENT ROW)
  1       3/1/2012     $10.00      Joe
  2       3/1/2012     $11.00      Sam




                                         PARTITION BY Cust
                                                             Cust   OrdDt      OrdID   Running
  3       3/2/2012     $10.00     Beth
  4       3/2/2012     $15.00      Joe                       Joe    3/1/2012   1       $10.00
  5       3/2/2012     $17.00      Sam                       Joe    3/2/2012   4       $25.00
  6       3/3/2012     $12.00      Joe
                                                             Joe    3/3/2012   6       $37.00
  7       3/4/2012     $10.00     Beth
                                                             Joe    3/4/2012   9       $49.00
  8       3/4/2012     $18.00      Sam
  9       3/4/2012     $12.00      Joe
SQL 2012 Window Illustrated (4)
Aggregate with Range Functionality
   select OrderID
   ,OrderDate
   ,OrderAmt
   ,CustomerName
   ,SUM(OrderAmt)
      OVER (PARTITION BY CustomerName
              ORDER BY OrderDate, OrderID
              RANGE BETWEEN UNBOUNDED PRECEDING
                 AND CURRENT ROW) as RunningTotalAmt
   from CTEOrders
    The results are the same as without the RANGE clause. This RANGE clause
represents the default window when the ORDER BY is specified, but RANGE is not.
More Detail on ROWS and RANGE
» UNBOUNDED key word is supported by both ROWS and RANGE
» ROWS function only supports the numbered FOLLOWING or PRECEDING
  » e.g. 1 FOLLOWING will only work with ROWS
» CURRENT ROW can be specified alone with different results
  » ROWS – only the current row is included in the partition, thus no
    aggregation
  » RANGE – honors the partition and order definitions to create a range
    based on the current row’s definition
SQL 2012 Window Illustrated (5)
Analytic Functionality
                                        Previous Row – Previous Order Amount:
OrdID   OrdDt      OrdAmt     Cust      LAG(OrderAmt)
                                        OVER (ORDER BY OrderDate, OrderID)
1       3/1/2012     $10.00      Joe
2       3/1/2012     $11.00      Sam             Cust    Prev Amt   Next Amt




                                        Sample
3       3/2/2012     $10.00      Beth




                                         Rows
4       3/2/2012     $15.00      Joe             Beth    $11.00     $10.00
5       3/2/2012     $17.00      Sam             Joe     $10.00     $12.00
6       3/3/2012     $12.00      Joe             Sam     $15.00     $18.00
7       3/4/2012     $10.00     Beth
8       3/4/2012     $18.00      Sam    Next Row – Next Customer Order Amount:
9       3/4/2012     $12.00      Joe    LEAD(OrderAmt) OVER (PARTITION BY
                                        CustomerName ORDER BY OrderDate, OrderID)
SQL 2012 Window Illustrated (5)
Analytic Functionality
  select OrderID
  ,OrderDate
  ,OrderAmt
  ,CustomerName
  ,LAG(OrderAmt)
     OVER (ORDER BY OrderDate, OrderID)
     as PrevOrderAmt
  ,LEAD(OrderAmt)
     OVER (PARTITION BY CustomerName
             ORDER BY OrderDate, OrderID)
     as NextOrderAmtForCustomer
  from CTEOrders
SQL 2012 Demos
» Ranking Windows
» Aggregate Windows
» New SQL 2012 Window Functionality
  » Aggregate with Order
  » Aggregate with ROW
  » Aggregate with RANGE
  » Analytic Functions
Wrap Up – Final Thoughts
» OVER() – defaults to the entire set
» Each refinement to the OVER clause changes the set of data that the
  window function is applied to, even ORDER BY
» Both the PARTITION BY and ORDER BY clauses support multiple columns
Wrap Up
» More Information
  » MSDN
    » OVER Clause: http://msdn.microsoft.com/en-us/library/ms189461(v=SQL.110).aspx
    » Analytic Functions: http://msdn.microsoft.com/en-
      us/library/hh213234(v=sql.110).aspx
  » LessThanDot Windows Function Series
    » http://dataonwheels.wordpress.com/2012/09/26/t-sql-window-functions-series-
      on-lessthandot/
» Questions?
Steve Hughes
»   Practice Lead with Magenic
»   Over 15 years with SQL Server
»   Chair of Minnesota SQL Server User Group (PASSMN)
»   PASS Regional Mentor for US-NorthEast Region

» Contact Info
  » Blog: http://www.dataonwheels.com
  » Twitter: @DataOnWheels
  » Email: steveh@magenic.com
Thanks for Attending

Contenu connexe

En vedette

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

En vedette (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Using SQL Window Functions to Gain Insights into Your Data

  • 1. A Window into Your Data Using SQL Window Functions Steve Hughes Practice Lead, Data and BI
  • 2. Agenda » SQL Window Functions Overview » SQL Server 2008 Functions » SQL Server 2012 Functions » Wrap Up
  • 3. What Are SQL Window Functions & Why Should We Care? » Introduced in 2005, expanded in 2012 » Used for grouping data for use at the row level » Allow for solving complex business questions » % of customer’s total sales » Running total » Next and previous row values » Does not require GROUP BY or subselect queries
  • 4. Structure of the OVER Clause » OVER – used to define the window over which the function will be applied. The default window is the entire set. » PARTITION BY – groups the data by one or more columns » ORDER BY – sorts the data in the partition » ROWS or RANGE – further reduces the size of the window based on proximity from the current row » OVER([PARTITION BY <<field list>>] [ORDER BY <<field list>>] [ROWS/RANGE <<set of rows>> ])
  • 5. SQL 2008 Functionality » Ranking window functions OVER([PARTITION BY value_expression, … [n]] <ORDER BY_Clause>) » ROW_NUMBER » DENSE_RANK » RANK » NTILE » Aggregate window functions OVER([PARTITION BY value_expression, … [n]]) » Most aggregate function, e.g. SUM, COUNT, AVG » Does not support GROUPING or GROUPING_BY aggregations » Does not support ORDER BY
  • 6. SQL 2008 Window Illustrated OrderID OrderDate OrderAmt Aggregation Window – DailyTotal: SUM(OrderAmt) OVER (PARTITION BY OrderDate) 1 3/1/2012 $10.00 2 3/1/2012 $11.00 OrderID DailyTotal DailyRank PARTITION BY OrderDate 3 3/2/2012 $10.00 4 3/2/2012 $15.00 3 $42.00 3 5 3/2/2012 $17.00 4 $42.00 2 6 3/3/2012 $12.00 5 $42.00 1 7 3/4/2012 $10.00 8 3/4/2012 $18.00 Ranking Window – DailyRank: 9 3/4/2012 $12.00 RANK() OVER (PARTITION BY OrderDate ORDER BY OrderAmt DESC)
  • 7. SQL 2008 SQL Comparison: SubSelect vs Window Function select (select sum(OrderAmt) from CTEOrders o where o.OrderDate = CTEOrders.OrderDate) as DailyOrderAmt from CTEOrders select SUM(OrderAmt) OVER (PARTITION BY OrderDate) from CTEOrders order by OrderID
  • 8. Why Not Group By? select OrderDate ,sum(OrderAmt) as DailyOrderAmt from CTEOrders group by OrderDate » This returns the amount and is the subselect logic select OrderDate ,OrderID ,OrderAmt ,sum(OrderAmt) as DailyOrderAmt from CTEOrders group by OrderDate ,OrderID ,OrderAmt » This returns the details, but the sum is not the correct
  • 9. One Twist on Group By select sum(OrderAmt) , sum(OrderAmt) over() as TotalOrderAmt from CTEOrders group by CustomerName » This returns an error because the first expression is an aggregate, but not the second » Also, OVER() is evaluated over the full set select sum(OrderAmt) , sum(sum(OrderAmt)) over() as TotalOrderAmt from CTEOrders group by CustomerName » This returns the aggregate of the aggregates and does not return an error » Thanks to Itzik Ben-Gan for this tip
  • 10. SQL 2008 Demos » Group By and SubSelect » Ranking Windows » Aggregate Windows » Twisted Group By
  • 11. SQL 2012 Functionality » Significantly expanded in this release » Greater ANSI compliance » Matches Oracle 11g » ORDER BY clause supported by all function types, not just ranking functions » ROWS and RANGE clauses added to OVER clause » This allows for running totals and other hard to resolve problems » New analytic functions including: » Position values (e.g. LAG, LAST_VALUE) » Percentile values and rank (e.g. PCT_RANK, PERCENTILE_DISC)
  • 12. SQL 2012 Window Illustrated (1) 2008 Functionality OrderID OrderDate OrderAmt Aggregation Window – DailyTotal: SUM(OrderAmt) OVER (PARTITION BY OrderDate) 1 3/1/2012 $10.00 2 3/1/2012 $11.00 OrderID DailyTotal DailyRank PARTITION BY OrderDate 3 3/2/2012 $10.00 4 3/2/2012 $15.00 3 $42.00 3 5 3/2/2012 $17.00 4 $42.00 2 6 3/3/2012 $12.00 5 $42.00 1 7 3/4/2012 $10.00 8 3/4/2012 $18.00 Ranking Window – DailyRank: 9 3/4/2012 $12.00 RANK() OVER (PARTITION BY OrderDate ORDER BY OrderAmt DESC)
  • 13. SQL 2012 Window Illustrated (2) Aggregate with Order By Functionality Aggregate Window – Running Total by Customer: SUM(OrderAmt) OVER OrdID OrdDt OrdAmt Cust (PARTITION BY CustomerName ORDER BY OrderDate, OrderID) 1 3/1/2012 $10.00 Joe 2 3/1/2012 $11.00 Sam PARTITION BY Cust Cust OrdDt OrdID RunTot 3 3/2/2012 $10.00 Beth 4 3/2/2012 $15.00 Joe Sam 3/1/2012 2 $11.00 5 3/2/2012 $17.00 Sam Sam 3/2/2012 5 $28.00 6 3/3/2012 $12.00 Joe 7 3/4/2012 $10.00 Beth Sam 3/4/2012 8 $46.00 8 3/4/2012 $18.00 Sam 9 3/4/2012 $12.00 Joe
  • 14. SQL 2012 Window Illustrated (2) Aggregate with Order By Functionality select OrderID ,OrderDate ,OrderAmt ,CustomerName ,SUM(OrderAmt) OVER (PARTITION BY CustomerName ORDER BY OrderDate, OrderID) as RunningByCustomer from CTEOrders
  • 15. SQL 2012 Window Illustrated (3) Aggregate Rows Window – Aggregate with Rows Functionality Next 2 Orders by Customer: SUM(OrderAmt) OVER (PARTITION BY CustomerName ORDER BY OrderDate, OrderID ROWS OrdID OrdDt OrdAmt Cust BETWEEN 1 FOLLOWING AND 2 FOLLOWING) 1 3/1/2012 $10.00 Joe 2 3/1/2012 $11.00 Sam Cust OrdDt OrdID NextTwo PARTITION BY Cust 3 3/2/2012 $10.00 Beth Joe 3/1/2012 1 $27.00 4 3/2/2012 $15.00 Joe 5 3/2/2012 $17.00 Sam Joe 3/2/2012 4 $24.00 6 3/3/2012 $12.00 Joe Joe 3/3/2012 6 $12.00 7 3/4/2012 $10.00 Beth Joe 3/4/2012 9 NULL 8 3/4/2012 $18.00 Sam 9 3/4/2012 $12.00 Joe
  • 16. SQL 2012 Window Illustrated (3) Aggregate with Rows Functionality select OrderID ,OrderDate ,OrderAmt ,CustomerName ,SUM(OrderAmt) OVER (PARTITION BY CustomerName ORDER BY OrderDate, OrderID ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING) as NextTwoAmts from CTEOrders ROWS clause sets the window based on physical proximity to current row.
  • 17. SQL 2012 Window Illustrated (4) Aggregate Range Window – Running Total by Customer: Aggregate with Range Functionality SUM(OrderAmt) OVER (PARTITION BY CustomerName ORDER BY OrderDate, OrderID OrdID OrdDt OrdAmt Cust RANGE BETWEEN UNBOUNDED PRECEDING and CURRENT ROW) 1 3/1/2012 $10.00 Joe 2 3/1/2012 $11.00 Sam PARTITION BY Cust Cust OrdDt OrdID Running 3 3/2/2012 $10.00 Beth 4 3/2/2012 $15.00 Joe Joe 3/1/2012 1 $10.00 5 3/2/2012 $17.00 Sam Joe 3/2/2012 4 $25.00 6 3/3/2012 $12.00 Joe Joe 3/3/2012 6 $37.00 7 3/4/2012 $10.00 Beth Joe 3/4/2012 9 $49.00 8 3/4/2012 $18.00 Sam 9 3/4/2012 $12.00 Joe
  • 18. SQL 2012 Window Illustrated (4) Aggregate with Range Functionality select OrderID ,OrderDate ,OrderAmt ,CustomerName ,SUM(OrderAmt) OVER (PARTITION BY CustomerName ORDER BY OrderDate, OrderID RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as RunningTotalAmt from CTEOrders The results are the same as without the RANGE clause. This RANGE clause represents the default window when the ORDER BY is specified, but RANGE is not.
  • 19. More Detail on ROWS and RANGE » UNBOUNDED key word is supported by both ROWS and RANGE » ROWS function only supports the numbered FOLLOWING or PRECEDING » e.g. 1 FOLLOWING will only work with ROWS » CURRENT ROW can be specified alone with different results » ROWS – only the current row is included in the partition, thus no aggregation » RANGE – honors the partition and order definitions to create a range based on the current row’s definition
  • 20. SQL 2012 Window Illustrated (5) Analytic Functionality Previous Row – Previous Order Amount: OrdID OrdDt OrdAmt Cust LAG(OrderAmt) OVER (ORDER BY OrderDate, OrderID) 1 3/1/2012 $10.00 Joe 2 3/1/2012 $11.00 Sam Cust Prev Amt Next Amt Sample 3 3/2/2012 $10.00 Beth Rows 4 3/2/2012 $15.00 Joe Beth $11.00 $10.00 5 3/2/2012 $17.00 Sam Joe $10.00 $12.00 6 3/3/2012 $12.00 Joe Sam $15.00 $18.00 7 3/4/2012 $10.00 Beth 8 3/4/2012 $18.00 Sam Next Row – Next Customer Order Amount: 9 3/4/2012 $12.00 Joe LEAD(OrderAmt) OVER (PARTITION BY CustomerName ORDER BY OrderDate, OrderID)
  • 21. SQL 2012 Window Illustrated (5) Analytic Functionality select OrderID ,OrderDate ,OrderAmt ,CustomerName ,LAG(OrderAmt) OVER (ORDER BY OrderDate, OrderID) as PrevOrderAmt ,LEAD(OrderAmt) OVER (PARTITION BY CustomerName ORDER BY OrderDate, OrderID) as NextOrderAmtForCustomer from CTEOrders
  • 22. SQL 2012 Demos » Ranking Windows » Aggregate Windows » New SQL 2012 Window Functionality » Aggregate with Order » Aggregate with ROW » Aggregate with RANGE » Analytic Functions
  • 23. Wrap Up – Final Thoughts » OVER() – defaults to the entire set » Each refinement to the OVER clause changes the set of data that the window function is applied to, even ORDER BY » Both the PARTITION BY and ORDER BY clauses support multiple columns
  • 24. Wrap Up » More Information » MSDN » OVER Clause: http://msdn.microsoft.com/en-us/library/ms189461(v=SQL.110).aspx » Analytic Functions: http://msdn.microsoft.com/en- us/library/hh213234(v=sql.110).aspx » LessThanDot Windows Function Series » http://dataonwheels.wordpress.com/2012/09/26/t-sql-window-functions-series- on-lessthandot/ » Questions?
  • 25. Steve Hughes » Practice Lead with Magenic » Over 15 years with SQL Server » Chair of Minnesota SQL Server User Group (PASSMN) » PASS Regional Mentor for US-NorthEast Region » Contact Info » Blog: http://www.dataonwheels.com » Twitter: @DataOnWheels » Email: steveh@magenic.com

Notes de l'éditeur

  1. ROWS are used to filter within the partition based on proximity to the starting row.