Data Analysis with SQL Window Functions

Slides:



Advertisements
Similar presentations
OLAP Services Business Intelligence Solutions. Agenda Definition of OLAP Types of OLAP Definition of Cube Definition of DMR Differences between Cube and.
Advertisements

Chapter 2 Querying a Database
Concepts of Database Management Sixth Edition
A Linear Regression Algorithm Using Windowing Functions KEVIN MCCARTY.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
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.
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.
DBI309: Using SQL Server 2012 Window Functions to Solve Common T-SQL Challenges Steven Wang MCITP – BI, Database Developer and DBA.
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
Session 1 Retrieving Data From a Single Table
An Refresher and How-To Profile Data using SQL
Analytic Window Functions
Introduction to SQL Server Analysis Services
Lecturer : Dr. Pavle Mogin
Using Partitions and Fragments
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
Window function performance
05 | Using Functions and Aggregating Data
Using Window Ranking, Offset, and Aggregate Functions
Aggregating Data Using Group Functions
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
Lecture#7: Fun with SQL (Part 2)
Using SQL to Prepare Data for Analysis
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
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
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Writing Better Queries with Window Functions
Enhance BI Applications and Simplify Development
Jan Engelsberg Program Manager, SQL Server
Oracle8i Analytical SQL Features
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
T-SQL Window function deep dive part 2
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Aggregating Data Using Group Functions
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Introduction to Window Functions
Introduction to T-sql Window functionS
Four Rules For Columnstore Query Performance
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
From and Report.
CS240B: Assignment1 Winter 2016.
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Slides based on those originally by : Parminder Jeet Kaur
Intermediate Query Structure and Development
T-SQL: Simple Changes That Go a Long Way
Presentation transcript:

Data Analysis with SQL Window Functions Good afternoon, let’s get started This class is called “Data Analysis with SQL Window Functions” We’re going to look at a high level overview of window functions and how to use them First an introduction Adam McDonald Senior developer at STR in nashville, 13+ years History: started out as a .NET developer (reporting applications, windows services, windows forms) started doing my own SQL work to increase efficiency SSIS (integration services) SSAS (analysis services) to preaggregate data (OLAP / MDX) over the years I became the resident data expert Now I build database solutions for the devs to use Adam McDonald Manager of Database Development STR adam@str.com @adamtmcdonald

Window Functions (not Windows) Window functions is singular Windows is an operating system, this is a window you’re opening a window into your result set

Window Functions (not Windows) Window functions is singular you’re defining a window into your result set

References: High-Performance T-SQL Using Window Functions By: Itzik Ben-Gan Todays presentation is available on dropbox. Slides : https://db.tt/Foc8fcfG Demo : https://db.tt/ghjllHYE

What are window functions? Window functions are the sliced bread of SQL server! "Window functions, to me, are the most profound feature supported by both standard SQL and Microsoft SQL Server's dialect -TSQL. They allow you to perform calculations against sets of rows in a flexible, clear, and efficient manner. The design of window functions is ingenious, overcoming a number of shortcomings of the traditional alternatives.” Itzik Ben-Gan you've heard the saying "This is the greatest thing since sliced bread.“ Window functions are the sliced bread of sql server When they were introduced it immediately changed the way certain problems were solved Gave a unified approach to doing certain tasks Read intro from book That’s a pretty powerful statement

What are window functions? Definition: Window functions allow you to perform calculations against sets or of rows within a query. subsets SQL Order of Operations FROM WHERE GROUP BY HAVING SELECT ORDER BY official definition Subsets is the key here Window functions allow you to get a result set and then drill into it to perform additional calculations order of operations imagine that window functions fit after the select clause what does that mean? only usable in the select and order by, not in WHERE clause Some of the third party players have solutions for that, like a qualify statement that works like a where clause This seems like a limitation but… You can usually work around that limitation by applying the window function in a CTE then referencing it in the where statement outside of the CTE Window functions are performed after the SELECT

Why are they useful / better? Fast Efficient Powerful Easy to read syntax Does not require GROUP BY or sub-queries Encourages you to use set based operations Fast & Efficient & Powerful But it does have a cost Imagine you built a CTE with a window function applied Then outside the CTE you filter based on the window function, what happens? It has to build the entire result and apply the function to every row before it can return a filtered set Easy to read syntax Does not require GROUP BY or sub-queries this can be really handy Group by clauses summarize data into a single context Window functions allow you to have multiple contexts in one query For example, if I’m pulling monthly sales data for hotels I can pull the row level monthly data, then using window functions add R3, R12, YTD All in one query and very fast Encourages you to use set based operations We all know in sql server you can’t get optimum performance without using set based operations

Version History SQL 2005 SQL 2012 SQL 2016 Not just SQL Server Ranking function Aggregate functions (only partition clause, no frame clause) SQL 2012 Window order clauses Frame clauses Offset clauses Distribution functions SQL 2016 Batch mode operators with Column Store Indexes Not just SQL Server Window functions are part of the SQL ANSI standard PostgreSQL, Oracle, MySQL, DB2, Teradata… Parts of the standard are not supported by Microsoft Originally added in sql 2005 with limited support 2012 was the first version where they were fully fleshed out Not just SQL server, included in SQL standards Some have slightly different implementations For example, Teradata created a Qualify statement Acts like a WHERE clause with a window function If you’re not using 2012 you’re limited, but still good to know

Function Types Aggregate Distribution / Analytic Ranking SUM MIN MAX COUNT / COUNT_BIG AVG Distribution / Analytic CUME_DIST FIRST_VALUE LAST_VALUE LAG LEAD PERCENTILE_CONT PERCENTILE_DISC PERCENT_RANK VAR / VARP STDEV / STDEVP Ranking ROW_NUMBER RANK DENSE_RANK NTILE Three general types of window functions I have typically used the functions on the left more but the ones on the right are good to know about For example, if you have a complete timeline, you could use LAG to calculate percent change

Parts of a Window Functions Name SUM(revenue) OVER( PARTITION BY hotelID ORDER BY yearmonth ROWS BETWEEN 2 PRECEDING AND CURRENT ROW ) Partition Clause Order Clause Window Definition Frame Clause Function name A lot of times you’ll have to provide a column to apply the function to For example, a sum on the revenue column You can also provide an expression, such as a calculation Over clause It’s not a window function without an OVER clause You use the over clause to define your window Remember you’re getting a result set then building a window on top of that set (the window definition is applied to each row) The over clause has several optional clauses inside it that you can use to specify your window Partition clause The partition clause allows you to apply a function to multiple smaller windows For example: imagine if you wanted a sum by year, you would partition by year Any record in the year 2000 would have a window of all the record in the year 2000 This ones easier to see in use than to explain, we’ll look at some examples in a minute Order clause Lets you sort your window just like you would sort your entire result set A good example is the RANK function With the RANK function you have to include an ORDER clause Otherwise the function wouldn’t know the order in which to rank them Frame clause The frame clause lets you navigate within the bounds of the window partition In this example, it’s saying (within my window *partition*) give me the current row and the 2 rows (if any) preceding it Does anyone know what the function on this slide is doing? Running 3 month sum Running 3 Month Sum

Demo:

Questions? Slides : https://db.tt/Foc8fcfG Demo : https://db.tt/ghjllHYE Adam McDonald Manager of Database Development STR adam@str.com @adamtmcdonald