More Windowing Functions KEVIN MCCARTY. What are Windowing Functions Again? Introduced in SQL Server 2005 (SQL 2003 Standard) Used to provide operations.

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.
Characteristic Functions. Want: YearCodeQ1AmtQ2AmtQ3AmtQ4Amt 2001e (from fin_data table in Sybase Sample Database) Have: Yearquartercodeamount.
Exploring Microsoft Access
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.
Putting the Sting in Hive Page 1 Alan F.
Computer Science 101 Web Access to Databases SQL – Extended Form.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
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.
Introduction to database systems
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.
Views Lesson 7.
Mark Inman U.S. Navy (Naval Sea Logistics Center) Session #213 Analytic SQL for Beginners.
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
What’s New In Denali - TSQL David Ballantyne. Who am I Kent.Net/SqlServer.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
T-SQL: Simple Changes That Go a Long Way DAVE ingeniousSQL.com linkedin.com/in/ingenioussql.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
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.
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.
Educational Research Descriptive Statistics Chapter th edition Chapter th edition Gay and Airasian.
Using Structured Query Language (SQL) NCCS Applications –MS Access queries (“show SQL”) –SAS (PROC SQL) –MySQL (the new dataserver) –Visual Foxpro Other.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Jeremy Kingry, eBECS | ADVANCED SQL SERVER FOR ADMINS AND ANALYSTS.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
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
T-SQL: Simple Changes That Go a Long Way
T-SQL: Simple Changes That Go a Long Way
Database Systems Subqueries, Aggregation
Data Analysis with SQL Window Functions
Advanced Teradata SQL GLOBAL Temporary Vs VOLATILE Temporary Vs Derived tables WITH and WITH BY Special Index function Trigger Online Analytical Function.
Window function performance
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
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
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
CS240B: Assignment1 Winter 2016.
Projecting output in MySql
OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Intermediate Query Structure and Development
T-SQL: Simple Changes That Go a Long Way
Presentation transcript:

More Windowing Functions KEVIN MCCARTY

What are Windowing Functions Again? Introduced in SQL Server 2005 (SQL 2003 Standard) Used to provide operations on subsets, subgroupings of data ◦This was a difficult operation before, requiring liberal use of temp tables, CTEs, subqueries and a lot of code ◦Often with a significant performance hit We have already discussed ranking functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE) We will finish our discussion by covering the other types of windowing functions ◦Aggregate (SUM, AVG, COUNT, ROWS, RANGE) ◦Analytic/Offset (LAG, LEAD, FIRST_VALUE, LAST_VALUE) ◦Distribution (PERCENT_RANK, CUME_DIST, PERCENTILE, DISC, PERCENTILE_CONT)

Quick Review - The OVER clause A core component of a windowing function is the OVER clause Use the OVER clause to define the “window” or specific set of rows to apply the windowing function ◦OVER must be combined with an ORDER BY clause (which makes sense) SELECT BusinessEntityID AS SalesID, FirstName + ' ' + LastName AS FullName, SalesLastYear, ROW_NUMBER() OVER(ORDER BY SalesLastYear ASC) AS RowNumber FROM Sales.vSalesPerson;

Partitioning Use Partitioning to limit sets of rows to those with the same value in a partitioning column ◦Another term for this is “framing” and is similar to the GROUP BY ◦Useful for isolating sets with specific characteristics without having to run a separate query SELECT BusinessEntityID AS SalesID, FirstName + ' ' + LastName AS FullName, SalesLastYear, TerritoryGroup, ROW_NUMBER() OVER(PARTITION BY TerritoryGroup ORDER BY SalesLastYear ASC) AS RowNumber FROM Sales.vSalesPerson;

ROWS/RANGE Functions Use the ROWS or RANGE functions to further refine a partition ◦Requires use of the ORDER BY functions ◦Rows or ranges are based upon proximity to the current row ◦Row functions ◦CURRENT ROW – current row in the resultset (ROWS and RANGE) ◦UNBOUNDED PRECEDING – the beginning of a partition (ROWS and RANGE) ◦UNBOUNDED FOLLOWING – the end of a partition (ROWS and RANGE) ◦n PRECEDING – number of rows before the current row (ROWS only) ◦n FOLLOWING – number of rows after the current row (ROWS only)

RANGE vs. ROWS

Windowing Components Conceptual relationship between window elements: Result set (OVER) Window partition (PARTITION BY) Frame (ROWS BETWEEN) Note: Shameless theft from Microsoft courseware

Framing CURRENT ROW is just what it says, the current row in the result set ◦You can bound from the current row to either the beginning of the set (UNBOUNDED PRECEDING) or some number in between (n PRECEDING)

Framing Cont. You can also bound to the end of the result set (UNBOUNDED FOLLOWING) or somewhere in between (n FOLLOWING) Note – shameless thievery from functions-in-sql-server-part-2-the-frame/ functions-in-sql-server-part-2-the-frame/

Offset/Analytic Functions There are four offset (also referred to as analytic) you can use to avoid having to do a bunch of self-joins ◦LAG – Apply a previous (1 or more) row ◦Lead – Apply a subsequent (1 or more) row

Offset/Analytic Functions There are four offset (also referred to as analytic) you can use to avoid having to do a bunch of self-joins ◦FIRST_VALUE – get value of first row for all records ◦LAST_VALUE – get value of last row for all records ◦Note – ORDER BY is required

Distribution Functions PERCENT_RANK – calculates as a percentage, the rank of a given row compared to the overall ranking ◦Useful to see where a row “scores” ◦CUME_DIST – calculates as a value where a row stands compared to the total ◦Use to see how far a row is compared to the total/bottom

Distribution Functions cont. SQL Server 2012 also introduced two inverse distribution functions PERCENTILE_DISC – determines where along a distribution curve (using a discrete model) a value lies PERCENTILE_CONT – determines where along a distribution curve (using a continuous model) a value lies Note that a discrete model uses actual values whereas a continuous model can extrapolate across a series of values

Distribution Functions PERCENT_RANK (percentile rank) CUME_DIST (cumulative distribution) PERCENTILE_DISC (percentile using discrete distribution model) PERCENTILE_CONT (percentile using continuous distribution model)

Basic Syntax OVER ( [ ] -- optional [ ] [ ] - optional ) SELECT BusinessEntityID AS SalesID, FirstName + ' ' + LastName AS FullName, SalesLastYear, ROW_NUMBER() OVER(ORDER BY SalesLastYear ASC) AS RowNumber FROM Sales.vSalesPerson;