T-SQL Window Functions in Microsoft SQL Server Denali

Slides:



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

Working with Tables 1 of 5. Working with Tables 2 of 5.
DBI310. “Rank orders by order value descending” “Return consecutive periods of activity” “Return the percent of the current order value out of the.
Ingres/Vectorwise Implementation Details XXV Ingres Benutzerkonferenz 2012 Confidential © 2011 Actian Corporation Doug Inkster 1 of 9.
Exploring Microsoft Access
Oracle Analytic SQL NCOUG 2008 By: Ron Warshawsky CTO DBA InfoPower, Inc.
Chapter 11 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.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Chapter 3 Single-Table Queries
Introduction to Databases Chapter 7: Data Access and Manipulation.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
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.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
© Jalal Kawash Database Queries Peeking into Computer Science.
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.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Chapter 8 Views and Indexes 第 8 章 视图与索引. 8.1 Virtual Views  Views:  “virtual relations”. Another class of SQL relations that do not exist physically.
Random Query Generator for Hive November 2015 Hive Contributor Meetup Szehon Ho.
Background Lots of Demos(That’s it.)
Aggregator  Performs aggregate calculations  Components of the Aggregator Transformation Aggregate expression Group by port Sorted Input option Aggregate.
©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.
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.
Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries.
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
Analytic Window Functions
Lecturer : Dr. Pavle Mogin
Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts
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.
Using Window Ranking, Offset, and Aggregate Functions
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
SQL FUNDAMENTALS CDSE Days 2018.
WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan
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
SQL – Entire Select.
Built in Functions Massaging the data.
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
Query Functions.
CS240B: Assignment1 Winter 2016.
OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
LINQ to SQL Part 3.
New Perspectives on Microsoft
T-SQL: Simple Changes That Go a Long Way
Presentation transcript:

T-SQL Window Functions in Microsoft SQL Server Denali Itzik Ben-Gan, SolidQ

Window Functions Window Functions, Described Limitations of Alternatives Windowing Concepts Window Functions Supported in SQL Server Optimization of Window Functions © 2011 Itzik Ben-Gan, SolidQ

Window Functions, Described Functions operating on a window (set) of rows defined by an OVER clause Types of functions: Ranking Aggregate Distribution SELECT empid, ordermonth, qty, SUM(qty) OVER(PARTITION BY empid ORDER BY ordermonth ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS runqty FROM Sales.EmpOrders; © 2011 Itzik Ben-Gan, SolidQ

Limitations of Alternatives Grouped queries: Force all calculations in the query to be done in context of groups Subqueries: Start from scratch Each requires separate visit to data Window functions: Starting point is underlying query’s result set (hence allowed only in SELECT and ORDER BY) Can utilize same access to data for multiple functions © 2011 Itzik Ben-Gan, SolidQ

Windowing Concepts Partitioning Ordering Framing © 2011 Itzik Ben-Gan, SolidQ

Partitioning Filters only rows with same values in partitioning elements SELECT orderid, custid, val, CAST(100. * val / SUM(val) OVER() AS NUMERIC(5, 2)) AS pctall, CAST(100. * val / SUM(val) OVER(PARTITION BY custid) AS NUMERIC(5, 2)) AS pctcust FROM Sales.OrderValues AS O1; © 2011 Itzik Ben-Gan, SolidQ

Ordering and Framing Window order clause gives meaning to framing Framing further filters rows within partition by identifying bounds Window frame units: ROWS: offset in terms of number of rows RANGE: offset in terms of values © 2011 Itzik Ben-Gan, SolidQ

Framing, ROWS SELECT empid, ordermonth, qty, SUM(qty) OVER(PARTITION BY empid ORDER BY ordermonth ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS runqty FROM Sales.EmpOrders; -- Same as: ROWS UNBOUNDED PRECEDING © 2011 Itzik Ben-Gan, SolidQ

Framing, ROWS SELECT empid, ordermonth, MAX(qty) OVER(PARTITION BY empid ORDER BY ordermonth ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS prvqty, qty AS curqty, ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) AS nxtqty, AVG(qty) OVER(PARTITION BY empid AND 1 FOLLOWING) AS avgqty FROM Sales.EmpOrders; © 2011 Itzik Ben-Gan, SolidQ

Framing, RANGE -- this example is not supported in Denali SELECT empid, ordermonth, qty, SUM(qty) OVER(PARTITION BY empid ORDER BY ordermonth RANGE BETWEEN INTERVAL '2' MONTH PRECEDING AND CURRENT ROW) AS sum3month FROM Sales.EmpOrders; SQL Server Denali supports only UNBOUNDED and CURRENT ROW delimiters When ORDER BY specified but no explicit frame, default is RANGE UNBOUNDED PRECEDING © 2011 Itzik Ben-Gan, SolidQ

Window Functions Supported in SQL Server SQL Server 2005 added initial support: Ranking calculations Aggregates with only window partitioning SQL Server Denali has more complete support: Aggregates with also window ordering and framing Offset functions: LAG, LEAD, FIRST_VALUE, LAST_VALUE Distribution functions: PERCENT_RANK, CUME_DIST, PERCENTILE_CONT, PERCENTILE_DISC © 2011 Itzik Ben-Gan, SolidQ

Examples for Aggregates -- running sum SELECT actid, tranid, val, SUM(val) OVER(PARTITION BY actid ORDER BY tranid ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS balance FROM dbo.Transactions; -- moving average AVG(val) OVER(PARTITION BY actid ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) AS movavg © 2011 Itzik Ben-Gan, SolidQ

Examples for Offset -- Diff between current order value and prev/next order values SELECT custid, orderid, orderdate, val, val - LAG(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid) AS diffprv, val - LEAD(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid) AS diffnxt FROM Sales.OrderValues; -- Diff between current order value and first/last order values val - FIRST_VALUE(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid) AS difffirst, val - LAST_VALUE(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS difflast © 2011 Itzik Ben-Gan, SolidQ

Examples for Rank Distribution rk = RANK, nr = count, np = rows precede or peer PERCENT_RANK = (rk - 1) / (nr - 1) CUME_DIST = np / nr -- Query Computing PERCENT_RANK and CUME_DIST SELECT testid, studentid, score, PERCENT_RANK() OVER(PARTITION BY testid ORDER BY score) AS percentrank, CUME_DIST() OVER(PARTITION BY testid ORDER BY score) AS cumedist FROM Stats.Scores; © 2011 Itzik Ben-Gan, SolidQ

Examples for Inverse Distribution PERCENTILE_DISC = percentile assuming discrete distribution model PERCENTILE_CONT = percentile assuming continuouse distribution model -- Query Computing PERCENTILE_DISC and PERCENTILE_CONT DECLARE @pct AS FLOAT = 0.5; SELECT testid, score, PERCENTILE_DISC(@pct) WITHIN GROUP(ORDER BY score) OVER(PARTITION BY testid) AS percentiledisc, PERCENTILE_CONT(@pct) WITHIN GROUP(ORDER BY score) OVER(PARTITION BY testid) AS percentilecont FROM Stats.Scores; © 2011 Itzik Ben-Gan, SolidQ

Optimization of Window Functions Indexing Guidelines POC Ascending partitioning values Fast Track with UNBOUNDED PRECEDING Expanding all frame rows When frame <= 4 rows When function isn’t cumulative, e.g., MIN, MAX When extreme diff between cur, top, bottom > 10,000 or using RANGE will use on-disk spool Compute two cumulative values Function is cumulative, e.g., SUM, COUNT Frame > 4 rows © 2011 Itzik Ben-Gan, SolidQ

More info… SolidQ.com, 5-day Advanced T-SQL Class InsideTSQL.com SQLMag.com © 2011 Itzik Ben-Gan, SolidQ

For attending this session and PASS SQLRally Nordic 2011, Stockholm THANK YOU! For attending this session and PASS SQLRally Nordic 2011, Stockholm