Introduction to T-sql Window functionS

Slides:



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

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.
Chapter 11 Group Functions
CSEN 5314 Quiz 4.
Introduction to Oracle9i: SQL1 SQL Group Functions.
©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.
Frequency Table Frequency tables are an efficient method of displaying data The number of cases for each observed score are listed Scores that have 0 cases.
Version 1.0. MCAD MCSD MCPD Enterprise SQL MCTS MCT Software/Web Development Consultant Cryptography/Digital Signature Consultant SQL Server 2005/2008R2/2012.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
BARBARIN DAVID SQL Server Senior Consultant Pragmantic SQL Server Denali : New development features.
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.
® Microsoft Office 2013 Access Maintaining and Querying a Database.
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.
Random Query Generator for Hive November 2015 Hive Contributor Meetup Szehon Ho.
Background Lots of Demos(That’s it.)
©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.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation OLAP Queries and SQL:1999.
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
Boost your T-SQL with the APPLY Operator
Analytic Window Functions
Lecturer : Dr. Pavle Mogin
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.
05 | Using Functions and Aggregating Data
Using Window Ranking, Offset, and Aggregate Functions
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
Sorting and Filtering Data
WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan
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
Indexing Fundamentals
Oracle8i Analytical SQL Features
Chapter 4 Summary Query.
T-SQL Window function deep dive part 2
Indexing for Beginners
Transact SQL Performance Tips
Introduction to Window Functions
T-SQL gotchas and power-ups
Data Analysis with 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.
Why You Should Consider Implementing Indexed Views
Intermediate Query Structure and Development
T-SQL: Simple Changes That Go a Long Way
Presentation transcript:

Introduction to T-sql Window functionS Kathi Kellenberger @auntkathi Kathi.kellenberger@linchpinpeople.com http://auntkathisql.com

Agenda What are T-SQL window functions? 2005 Ranking functions Window aggregates 2012 Enhancements Accumulating window aggregates Framing Offset functions Statistical functions Agenda

What are window functions? Not OS, based on ANSI-SQL standards 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 ORDER BY is required in OVER clause

ROW_NUMBER() example CustomerID OrderID Total ROW_NUMBER() OVER(PARTITION BY CustomerID Order by OrderID) 1 101 100 105 2000 2 106 300 3 102 40 103 11 104 432 4 107 674 109 234 110 889 5 108 76 111

Demo 1 Ranking functions

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 No ORDER BY Partition by OR use the Empty Over Clause ∑

Window Aggregate example CustomerID OrderID TotalDue SUM(TotalDue) OVER(Partition by CustomerID) 1 101 100 2400 102 2000 103 300 2 104 40 51 105 11 3 106 432 4 107 674 1797 108 234 109 889 5 110 76 310 111

Demo 2 Window aggregates

2012 enhancements Accumulation aggregates Even further define the window with FRAMING Eight new functions Offset Statistical

Accumulating aggregate example CustomerID OrderID TotalDue SUM(TotalDue) OVER(Order by OrderID) 1 101 100 2 102 40 140 103 11 151 3 104 432 583 105 2000 2583 106 300 2883 4 107 674 3557 5 108 76 3633 109 234 3867 110 889 4756 111 4990

Accumulating aggregates Demo 3 Accumulating aggregates

FRAMING: ROWS and RANGE Further define the window Each row has its own window Supported for specific function types

Framing: Vocabulary Term Used for ROWS Positional operator used to define the frame RANGE Logical operator used to define the frame. Not fully supported. The default if ROWS is not specified UNBOUNDED PRECEDING The first row of the partition UNBOUNDED FOLLOWING The last row of the partition CURRENT ROW The row where the calculation is being performed

FRAMING ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ROWS UNBOUNDED PRECEDING

FRAMING ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING

FRAMING ROWS BETWEEN 2 PRECEDING AND CURRENT ROW 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW

Demo 4 Framing

OFFSET Functions LAG() Grab a column from a previous row LEAD() Grab a column from a later row FIRST_VALUE() Grab a column from the first row Supports framing LAST_VALUE() Grab a column from the last row Supports framing -- be careful!!

LAG example CustomerID OrderID TotalDue LAG(TotalDue) OVER(ORDER BY OrderID) 1 101 100 NULL 2 102 40 103 11 3 104 432 105 2000 106 300 4 107 674 5 108 76 109 234 110 889 111

LEAD example CustomerID OrderID TotalDue LEAD(TotalDue) OVER(ORDER BY OrderID) 1 101 100 40 2 102 11 103 432 3 104 2000 105 300 106 674 4 107 76 5 108 234 109 889 110 111 NULL

First_VALUE example CustomerID OrderID TotalDue FIRST_VALUE (TotalDue) OVER(ORDER BY OrderID) 1 101 100 2 102 40 103 11 3 104 432 105 2000 106 300 4 107 674 5 108 76 109 234 110 889 111

LAST_VALUE example CustomerID OrderID TotalDue LAST_VALUE(TotalDue) OVER(ORDER BY OrderID) 1 101 100 234 2 102 40 103 11 3 104 432 105 2000 106 300 4 107 674 5 108 76 109 110 889 111

Demo 5 Offset functions

Statistical functions PERCENT_RANK() Relative rank. “My score is better than 90% of the scores” CUME_DIST() Cumulative distribution over a group of rows. “My score is at 90%” PERCENTILE_DISC() Computes a specific percentile PERCENTILE_CONT() Computes an exact percentile

PERCENT_RANK example

CUME_DIST example

PERCENTILE_DISC example

PERCENTILE_CONT example

Statistical functions Demo 6 Statistical functions

Resources My book: Expert T-SQL Window Functions Itzik Ben-Gan’s book: Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions http://www.auntkathisql.com My Pluralsight course