Introduction to Window Functions

Slides:



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

DBI310. “Rank orders by order value descending” “Return consecutive periods of activity” “Return the percent of the current order value out of the.
5.1Database System Concepts - 6 th Edition Chapter 5: Advanced SQL Advanced Aggregation Features OLAP.
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.
The Model Clause explained Tony Hasler, UKOUG Birmingham 2012 Tony Hasler, Anvil Computer Services Ltd.
Computing & Information Sciences Kansas State University Monday, 26 Nov 2007CIS 560: Database System Concepts Lecture 37 of 42 Monday, 26 November 2007.
Oracle Analytic Functions for IR Analysis and Reporting Mingguang Xu and Denise Gardner Office of Institutional Research University of Georgia.
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.
Computing & Information Sciences Kansas State University Wednesday, 29 Nov 2006CIS 560: Database System Concepts Lecture 39 of 42 Wednesday, 29 November.
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
The number which appears most often in a set of numbers. Example: in {6, 3, 9, 6, 6, 5, 9, 3} the Mode is 6 (it occurs most often). Mode : The middle number.
Session id: Darrell Hilliard Senior Delivery Manager Oracle University Oracle Corporation.
Random Query Generator for Hive November 2015 Hive Contributor Meetup Szehon Ho.
Analyzing Your Data with Analytic Functions Carl Dudley University of Wolverhampton, UK UKOUG Council Oracle ACE Director
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.
1 Ch. 11: Grouping Things Together  ANSI standard SQL Group functions: AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE  Others: 8i: GROUPING (used with CUBE.
DATABASES
DBI309: Using SQL Server 2012 Window Functions to Solve Common T-SQL Challenges Steven Wang MCITP – BI, Database Developer and DBA.
High Performance Statistical Queries. Sponsors Agenda  Introduction  Descriptive Statistics  Linear dependencies  Continuous variables  Discrete.
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
Percentage cube queries Optimisation Presented by: Abdallah KHELIL
Analytic Window Functions
Introduction to SQL Server Analysis Services
02 | Advanced SELECT Statements
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
Window function performance
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Introduction to Analysis Services 2008 R2 Cubes
05 | Using Functions and Aggregating Data
Using Window Ranking, Offset, and Aggregate Functions
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan
T-SQL Window Functions in Microsoft SQL Server Denali
WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan
T-SQL Power! The OVER Clause: Your Key to No-Sweat Problem Solving
T-SQL Window Function Deep Dive part 1
Introduction to partitioning
Writing Better Queries with Window Functions
Jan Engelsberg Program Manager, SQL Server
Oracle8i Analytical SQL Features
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
T-SQL Window function deep dive part 2
Transact SQL Performance Tips
T-SQL gotchas and power-ups
Data Analysis with SQL Window Functions
Introduction to T-sql Window 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:

Introduction to Window Functions Kathi Kellenberger, MVP Linchpin People auntkathisql.com

History 2005: Window functions introduced 2012: Window functions enhanced

Agenda What are window functions? Ranking functions Window aggregates 2012 Enhancements: ORDER BY 2012 Enhancements: Framing Analytic Functions Offset Functions Statistical Functions Making Business Intelligent

What are window functions? Function performs over a SET (window of the results) How to identify a window function The OVER clause defines the window Where to put window functions SELECT and ORDER BY clauses only Important! The FROM, WHERE, GROUP BY and HAVING clauses operate BEFORE the window functions Partitions NOT the same as GROUP BY

Ranking functions Available since 2005 ROW_NUMBER() A unique # over the window RANK() Deals with ties DENSE_RANK() NTILE() Divide rows into buckets

ROW_NUMBER() example CustomerID OrderID Total Row_Number() Over(Order by OrderID) 1 101 100 2 102 40 103 11 3 104 432 4 105 2000 5 106 300 6 107 674 7 108 76 8 109 234 9 110 889 10

ROW_NUMBER() example CustomerID OrderID TotalDue Row_Number() Over(Partition by CustomerID Order by OrderID) 1 100 5 2000 2 6 300 3 40 11 4 432 7 674 9 234 10 889 8 76

Ranking Functions Demo

Window aggregate functions Your favorite aggregates with no GROUP BY Add aggregate function to a non-aggregate query Calculate over the window: the entire result set or partition Pre-2012 functionality NOT optimized for performance

Window aggregate example CustomerID OrderID TotalDue Sum(TotalDue) Over() 1 100 4990 5 2000 6 300 2 40 3 11 4 432 7 674 9 234 10 889 8 76

Window aggregate example CustomerID OrderID TotalDue Sum(TotalDue) Over(Partition by CustomerID) 1 100 2400 5 2000 6 300 2 40 51 3 11 4 432 7 674 1797 9 234 10 889 8 76 310

Window Aggregates Demo

2012 enhancements ORDER BY clause added to window aggregates Performance optimization Even further define the window with ROWS and RANGE: FRAMING

Window aggregate ORDER BY example CustomerID OrderID TotalDue Sum(TotalDue) Over(Order by OrderID) 1 100 2 40 140 3 11 151 4 432 583 5 2000 2583 6 300 2883 7 674 3557 8 76 3633 9 234 3867 10 889 4756 4990

Running Total Demo

FRAME: ROWS and RANGE Further define window Terms UNBOUNDED PRECEDING UNBOUNDED FOLLOWING CURRENT ROW # PRECEDING # FOLLOWING RANGE is not fully implemented and can hurt performance

Framing Rows between unbounded preceding and current row

Framing Rows between current row and unbounded following

Framing Rows between 2 preceding and current row

Framing Rows between 5 preceding and 2 following

Framing Demo

The Analytic Functions LAG() and LEAD() Look back or look forward FIRST_VALUE() and LAST_VALUE() The very first or very last PERCENT_RANK() and CUME_DIST() Calculate ranking PERCENTILE_DISC() and PERCENTILE_CONT() Given a percent, find the value

Analytic Functions Demo

Resources https://www.pluralsight.com/courses/tsql-window-functions