Download presentation
Presentation is loading. Please wait.
Published byGervase Bates Modified over 6 years ago
1
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
2
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.
3
WINDOWING FUNCTIONS SHEDDING SOME LIGHT
4
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
5
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
6
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
7
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
8
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
9
You Can’t Do That RESTRICTIONS Help QUESTIONS
10
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
11
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
12
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
13
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?
14
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?
15
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?
16
The Over Clause DEMO Do You Want To Know A Secret?
17
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
18
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
19
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
20
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
21
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
22
Ranking Functions and the Order By Clause
DEMO EIGHT DAYS A WEEK
23
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
24
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
25
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
26
The Partition By Clause
DEMO WITH A LITTLE HELP FROM MY FRIENDS
27
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
28
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
29
LEAD and LAG DEMO WITH A LITTLE HELP FROM MY FRIENDS
30
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
31
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
32
ROWS and RANGES DEMO WITH A LITTLE HELP FROM MY FRIENDS
33
Group By Aggregations of Aggregations COME TOGETHER
Perform windowing functions on Aggregations SUM(COUNT()) MAX(AVG()) COME TOGETHER
34
Group By DEMO COME TOGETHER
35
Restrictions YOU CAN’T DO THaT
36
Restrictions Only in Select Statement YOU CAN’T DO THaT
No where row_order() = 1 YOU CAN’T DO THaT
37
Should I have called this slide
Questions Questions Should I have called this slide FROM ME TO YOU? @JamesMcG_MSBI TheJimmyRSA HELP
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.