Presentation is loading. Please wait.

Presentation is loading. Please wait.

WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan

Similar presentations


Presentation on theme: "WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan"— Presentation transcript:

1 WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan Dular @dejandular
Dejan Dular SQL Grillen, Lingen, 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

3 SUM(quantity) OVER (<this set of rows>)
Betsy

4 SUM(quantity) OVER (<partition> <order> <frame>)
Betsy

5 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

6 Available functions Ranking: ROW_NUMBER, RANK, DENSE_RANK, NTILE
Magic castle

7 Available functions Ranking: ROW_NUMBER, RANK, DENSE_RANK, NTILE
Aggregates: SUM, AVG, MIN, MAX, COUNT, COUNT_BIG, STDEV, STDEVP, VAR, VARP Magic castle

8 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

9 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

10 FRAMING ROWS (Physical operator) n PRECEEDING n FOLLLOWING
UNBOUNDED PRECEEDING UNBOUNDED FOLLOWING CURRENT ROW

11 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

12 FRAMING ROWS (Physical operator) n PRECEEDING n FOLLLOWING
UNBOUNDED PRECEEDING UNBOUNDED FOLLOWING CURRENT ROW RANGE (Logical operator) CURRENT ROW (what is current row?)

13 RANGE CURRENT ROW ROW DATA UNBOUNDED PRECEEDING UNBOUNDED FOLLOWING Charlie

14 Martha the Unicorn says:
Always define the window range to prevent invisible bugs in your queries

15 Performance LEAF INDEX
SELECT SUM(Sales) OVER (PARTITION BY Year ORDER BY Month ROWS UNBOUNDED PRECEDING) LEAF INDEX Lightning

16 Using window functions like a boss!
German sausage is the Wurst BUT! Austrian schnitzel is the Wiener! Using window functions like a boss!

17


Download ppt "WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan"

Similar presentations


Ads by Google