I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE 1964.01.18
A legacy of astonishing dominance The Beatles in the Top 100 A legacy of astonishing dominance 585 313 68 19 Over more than 32 years, The Beatles had 585 entries in the Billboard Top 100. In that time, at least one song by the Beatles was on the Top 100 chart in 313 different weeks. John, Paul, George and Ringo produced 68 different songs which spent time in the Billboard Top 100. From I Want to Hold Your Hand to The Long and Winding Road, 19 different Beatles singles spent at least a week on top of the charts.
WINDOWING FUNCTIONS SHEDDING SOME LIGHT
James McGillivray / BI Architect About Me Hey Jude James Professional 10 Years BI Experience Microsoft Certified SQL User Group Johannesburg, South Africa Personal Sport: Cricket, Soccer, Squash, Running Slowly. Culture: Symphony Choir of Johannesburg, Musical Theatre Other: Board Games, Poker James McGillivray / BI Architect @JamesMcG_MSBI TheJimmyRSA
The Long and Winding Road Where we’re going What Goes On SESSION OBJECTIVES INTRODUCTION TO WINDOWING FUNCTIONS Do You Want To Know A Secret THE OVER CLAUSE
The Long and Winding Road Where we’re going What Goes On SESSION OBJECTIVES INTRODUCTION TO WINDOWING FUNCTIONS Do You Want To Know A Secret THE OVER CLAUSE
Eight Days a Week With a Little Help From My Friends Yesterday RANKING FUNCTIONS AND ORDER BY With a Little Help From My Friends PARTITION BY Yesterday LEAD AND LAG ROWS AND RANGES Come Together GROUP BY
Eight Days a Week With a Little Help From My Friends Yesterday RANKING FUNCTIONS AND ORDER BY With a Little Help From My Friends PARTITION BY Yesterday LEAD AND LAG ROWS AND RANGES Come Together GROUP BY
You Can’t Do That RESTRICTIONS Help QUESTIONS
Visualise the Concepts Demonstrate the Functionality Session Objectives Learn the Syntax We’ll go through each clause available in Windowing functions, building from simplest to most complex Visualise the Concepts Demonstrate the Functionality After each clause is explained, see it demonstrated in an abstract but simple way. See the incredible power of the functions as we explore the music of THE BEATLES. WHAT GOES ON
Introduction to Windowing Functions Introduced in SQL 2012 Although RANKING functions already existed Ranking, Aggregation, Analytical Windowing functions reduce query complexity for common patterns Avoid Self-Joins Many queries can get rid of self-joins by using windowing functions WHAT GOES ON
Introduction to Windowing Functions 10 50 100 1000 1 5 9 13 20 100 150 100 2 6 10 14 30 200 200 50 3 7 11 15 40 50 1000 50 4 8 12 16 Demo Table (RowID, Colour, Value) WHAT GOES ON
The Over Clause Aggregations without Group By? Aggregation is performed OVER all rows in the data set Add Information Result Set Use data from the entire data set on each row in the set Totals Calculate totals over the set. Total # of Weeks Beatles songs spent on chart. Other Uses % of Total Average of Entire Set Do You Want To Know A Secret?
The Over Clause count(*) over () 16 Do You Want To Know A Secret? 10 50 100 1000 16 20 100 150 100 30 200 200 50 40 50 1000 50 count(*) over () Do You Want To Know A Secret?
The Over Clause sum(Value) over () Do You Want To Know A Secret? 3150 10 50 100 1000 3150 20 100 150 100 30 200 200 50 40 50 1000 50 sum(Value) over () Do You Want To Know A Secret?
The Over Clause DEMO Do You Want To Know A Secret?
Ranking Functions and the Order By Clause Position of Current Row in Set Must have an order by to determine sorting row_number() Ignore ties. Specify multiple columns in order by to avoid unexpected behaviour rank() Assign ties the same value. Skip values after ties dense_rank() Assign ties the same value. Do not skip value after ties. EIGHT DAYS A WEEK
Ranking Functions and the Order By Clause 10 50 100 1000 1 5 10 16 20 100 150 100 2 9 12 11 30 200 200 50 3 13 14 7 40 50 1000 50 4 6 15 8 row_number() over (order by VALUE) EIGHT DAYS A WEEK
Ranking Functions and the Order By Clause 10 50 100 1000 1 7 10 16 20 100 150 100 2 9 12 11 30 200 200 50 3 13 14 8 40 50 1000 50 4 6 15 5 row_number() over (order by VALUE) EIGHT DAYS A WEEK
Ranking Functions and the Order By Clause 10 50 100 1000 1 1 8 12 20 100 150 100 1 6 8 14 30 200 200 50 1 6 8 14 40 50 1000 50 1 8 12 14 rank() over (order by COLOUR) EIGHT DAYS A WEEK
Ranking Functions and the Order By Clause 10 50 100 1000 1 1 3 4 20 100 150 100 1 2 3 5 30 200 200 50 1 2 3 5 40 50 1000 50 1 3 4 5 dense_rank() over (order by COLOUR) EIGHT DAYS A WEEK
Ranking Functions and the Order By Clause DEMO EIGHT DAYS A WEEK
The Partition By Clause Split the data into groups Perform aggregation over the partitions Sub-Totals Totals per Category Other Uses Percentage of Sub-Total Ranking within group WITH A LITTLE HELP FROM MY FRIENDS
The Partition By Clause 10 50 100 1000 150 150 500 2000 20 100 150 100 150 300 500 250 30 200 200 50 150 300 500 250 40 50 1000 50 150 500 2000 250 sum(VALUE) over (partition by COLOUR) WITH A LITTLE HELP FROM MY FRIENDS
The Partition By Clause 10 50 100 1000 1 5 2 2 20 100 150 100 2 1 3 3 30 200 200 50 3 2 4 2 40 50 1000 50 4 1 1 1 row_number() over (partition by COLOUR order by VALUE) WITH A LITTLE HELP FROM MY FRIENDS
The Partition By Clause DEMO WITH A LITTLE HELP FROM MY FRIENDS
LEAD and LAG/ROWS and RANGES Order data within groups Must use the Order By clause with ROWS Calculate relative to row order All before this row. 1 after this row etc. Analysis within subset of partition Previous 3 Months Average Sales Year-on-Year Variance WITH A LITTLE HELP FROM MY FRIENDS
LEAD and LAG 10 50 100 1000 NULL 40 50 1000 20 100 150 100 10 NULL 100 NULL 30 200 200 50 20 100 150 100 40 50 1000 50 30 NULL NULL 50 lag(VALUE,1,NULL) over (partition by COLOUR order by ROWID) WITH A LITTLE HELP FROM MY FRIENDS
LEAD and LAG DEMO WITH A LITTLE HELP FROM MY FRIENDS
ROWS and RANGES sum(VALUE) over (partition by COLOUR order by POSITION 10 50 100 1000 10 150 150 2000 20 100 150 100 30 100 300 100 30 200 200 50 60 300 500 150 40 50 1000 50 100 50 1000 200 sum(VALUE) over (partition by COLOUR order by POSITION ROWS UNBOUNDED PRECEDING) WITH A LITTLE HELP FROM MY FRIENDS
ROWS and RANGES 10 50 100 1000 10 90 150 2000 20 100 150 100 30 100 250 100 30 200 200 50 50 300 350 150 40 50 1000 50 70 50 1000 100 sum(VALUE) over (partition by COLOUR order by POSITION ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) WITH A LITTLE HELP FROM MY FRIENDS
ROWS and RANGES DEMO WITH A LITTLE HELP FROM MY FRIENDS
Group By Aggregations of Aggregations COME TOGETHER Perform windowing functions on Aggregations SUM(COUNT()) MAX(AVG()) COME TOGETHER
Group By DEMO COME TOGETHER
Restrictions YOU CAN’T DO THaT
Restrictions Only in Select Statement YOU CAN’T DO THaT No where row_order() = 1 YOU CAN’T DO THaT
Should I have called this slide Questions Questions Should I have called this slide FROM ME TO YOU? @JamesMcG_MSBI TheJimmyRSA HELP