WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan

Slides:



Advertisements
Similar presentations
Analytic Functions : An Oracle Developer’s Best Friend
Advertisements

Oracle Analytic SQL NCOUG 2008 By: Ron Warshawsky CTO DBA InfoPower, Inc.
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Extended Aggregation SQL-92 aggregation quite limited  Many useful aggregates.
Copyright: Silberschatz, Korth and Sudarshan 1 OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Putting the Sting in Hive Page 1 Alan F.
Oracle 10g analytical SQL for Business Intelligence Reporting Simay Alpoge Next Information Systems, Inc. Next Information Systems, Inc.
A Linear Regression Algorithm Using Windowing Functions KEVIN MCCARTY.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
 Continue queries ◦ You completed two tutorials with step-by-step instructions for creating queries in MS Access. ◦ Now must apply knowledge and skills.
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Oracle Analytic Functions for IR Analysis and Reporting Mingguang Xu and Denise Gardner Office of Institutional Research University of Georgia.
Mark Inman U.S. Navy (Naval Sea Logistics Center) Session #213 Analytic SQL for Beginners.
What’s New In Denali - TSQL David Ballantyne. Who am I Kent.Net/SqlServer.
T-SQL: Simple Changes That Go a Long Way DAVE ingeniousSQL.com linkedin.com/in/ingenioussql.
More Windowing Functions KEVIN MCCARTY. What are Windowing Functions Again? Introduced in SQL Server 2005 (SQL 2003 Standard) Used to provide operations.
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
V 1.0 DBMAN 3 Group By, Having Cube, Rollup OLTP vs OLAP Data analysis 1.
Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
Janet works on the Azure Stream Analytics team, focusing on the service and portal UX. She has been in the data space at Microsoft for 5 years, previously.
Random Query Generator for Hive November 2015 Hive Contributor Meetup Szehon Ho.
Background Lots of Demos(That’s it.)
Sofia, Bulgaria | 9-10 October SQL Querying Tips & Techniques Richard Campbell.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Recursive Queries.
A Glance at the Window Functions. Window Functions Introduced in SQL 2005 Enhanced in SQL 2012 So-called because they operate on a defined portion of.
DATABASES
High Performance Statistical Queries. Sponsors Agenda  Introduction  Descriptive Statistics  Linear dependencies  Continuous variables  Discrete.
Southern Methodist University CSE CSE 2337 Introduction to Data Management Chapter 2.
Jeremy Kingry, eBECS | ADVANCED SQL SERVER FOR ADMINS AND ANALYSTS.
Eugene Meidinger Intermediate Querying: Going Beyond Select
T-SQL Power! Windows That Open Doors Adam
Data Analysis with SQL Window Functions Adam McDonald IT Architect / Senior SQL Developer Smith Travel
CS3220 Web and Internet Programming More SQL
Analytic Window Functions
T-SQL: Simple Changes That Go a Long Way
T-SQL: Simple Changes That Go a Long Way
What is New for T-SQL in SQL Server 2012 & SQL Azure
Database Systems Subqueries, Aggregation
Data Analysis with SQL Window Functions
Objectives Describe an overview of Transact-SQL programming
Window function performance
Chapter 5: Advanced SQL Database System concepts,6th Ed.
05 | Using Functions and Aggregating Data
In the name of the resources
Using Window Ranking, Offset, and Aggregate Functions
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
T-SQL Window Functions in Microsoft SQL Server Denali
WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan
T-SQL Window Function Deep Dive part 1
Writing Better Queries with Window Functions
Jan Engelsberg Program Manager, SQL Server
Oracle8i Analytical SQL Features
In the name of the resources
Aggregations Various Aggregation Functions GROUP BY HAVING.
T-SQL Window function deep dive part 2
Introduction to Window Functions
T-SQL gotchas and power-ups
Data Analysis with SQL Window Functions
Introduction to T-sql Window functionS
SQL Aggregation.
In the name of the resources - aggregating data
T-SQL Performance Tuning
Query Functions.
CS240B: Assignment1 Winter 2016.
Fewer cursors since SQL Server 2012 Came Along
OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Build on-the-fly reporting with Dynamic SQL
Intermediate Query Structure and Development
T-SQL: Simple Changes That Go a Long Way
Presentation transcript:

WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan Dular @dejandular dejan.dular@gmail.com Dejan Dular SQL Grillen, Lingen, 2017

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

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

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

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 1.352.577,13 David Campbell 1.573.012,94 2 Tete Mensa-Annan 1.576.562,20 3 Michael Blythe 3.763.178,18 Jillian Carson 3.189.418,37 4 Shu Ito 2.458.535,62 Linda Mitchell 4.251.368,55 PARTITION Buttercup

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

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

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

FRAMING Sarka Year Month Sales 2016 1 1.352.577,13 2 1.573.012,94 3 1.576.562,20 4 3.063.178,18 5 3.189.418,37 6 2.458.535,62 7 4.251.368,55 Sarka

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

1 2 3 4 5 6 7 8 9 10 ROWS CURRENT ROW UNBOUNDED PRECEEDING 1 2 3 4 5 6 7 8 9 10 UNBOUNDED PRECEEDING UNBOUNDED FOLLOWING 2 PRECEEDING 3 FOLLOWING 1 FOLLOWING UNBOUNDED FOLLOWING Rob

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

RANGE CURRENT ROW ROW 1 2 3 4 5 6 7 8 9 10 DATA 10 20 30 30 30 30 40 40 60 70 UNBOUNDED PRECEEDING UNBOUNDED FOLLOWING Charlie

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

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

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