Download presentation
Presentation is loading. Please wait.
1
WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan Dular @dejandular
Dejan Dular SQL Saturday Manchester, July 2017
2
Dejan Dular s.p. Dejan Dular Dejan Dular Dejan Dular Developer CEO CFO
.NET Developer SQL Developer Dynamics CRM specialist Dejan Dular CEO Dejan Dular CFO
4
SUM(quantity) OVER (<this set of rows>)
Betsy
5
SUM(quantity) OVER (<partition> <order> <frame>)
Betsy
6
SELECT S.TerritoryID, P.FirstName, P.LastName, S.SalesYTD,
ROW_NUMBER() OVER (PARTITION BY S.TerritoryId ORDER BY S.SalesYTD) FROM Sales.SalesPerson S JOIN Person.Person P ON S.BusinessEntityID = P.BusinessEntityID ORDER TerritoryID FirstName LastName SalesYTD Row Number 1 Pamela Ansman-Wolfe ,13 David Campbell ,94 2 Tete Mensa-Annan ,20 3 Michael Blythe ,18 Jillian Carson ,37 4 Shu Ito ,62 Linda Mitchell ,55 PARTITION Buttercup
7
Available functions Ranking: ROW_NUMBER, RANK, DENSE_RANK, NTILE
Magic castle
8
Available functions Ranking: ROW_NUMBER, RANK, DENSE_RANK, NTILE
Aggregates: SUM, AVG, MIN, MAX, COUNT, COUNT_BIG, STDEV, STDEVP, VAR, VARP Magic castle
9
Available functions Ranking: ROW_NUMBER, RANK, DENSE_RANK, NTILE
Aggregates: SUM, AVG, MIN, MAX, COUNT, COUNT_BIG, STDEV, STDEVP, VAR, VARP Analytic: FIRST_VALUE, LAST_VALUE, LAG, LEAD, PERCENT_RANK, CUME_DIST, PERCENTILE_CONT, PERCENTILE_DIST Magic castle
10
FRAMING Sarka Year Month Sales 2016 1 1.352.577,13 2 1.573.012,94 3
,20 4 ,18 5 ,37 6 ,62 7 ,55 Sarka
11
FRAMING ROWS (Physical operator) n PRECEEDING n FOLLLOWING
UNBOUNDED PRECEEDING UNBOUNDED FOLLOWING CURRENT ROW
12
1 2 3 4 5 6 7 8 9 10 ROWS CURRENT ROW UNBOUNDED PRECEEDING
UNBOUNDED PRECEEDING UNBOUNDED FOLLOWING 2 PRECEEDING 3 FOLLOWING 1 FOLLOWING UNBOUNDED FOLLOWING Rob
13
FRAMING ROWS (Physical operator) n PRECEEDING n FOLLLOWING
UNBOUNDED PRECEEDING UNBOUNDED FOLLOWING CURRENT ROW RANGE (Logical operator) CURRENT ROW (what is current row?)
14
RANGE CURRENT ROW ROW DATA UNBOUNDED PRECEEDING UNBOUNDED FOLLOWING Charlie
15
Martha the Unicorn says:
Always define the window range to prevent invisible bugs in your queries
16
Performance LEAF INDEX
SELECT SUM(Sales) OVER (PARTITION BY Year ORDER BY Month ROWS UNBOUNDED PRECEDING) LEAF INDEX Lightning
17
Using window functions like a boss!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.