Data Analysis with SQL Window Functions

Slides:



Advertisements
Similar presentations
MICROSOFT OFFICE ACCESS 2007.
Advertisements

OLAP Services Business Intelligence Solutions. Agenda Definition of OLAP Types of OLAP Definition of Cube Definition of DMR Differences between Cube and.
Database Systems More SQL Database Design -- More SQL1.
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.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
BY SATHISH SQL Basic. Introduction The language Structured English Query Language (SEQUEL) was developed by IBM Corporation, Inc., to use Codd's model.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
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.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
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.
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
More SQL: Complex Queries, Triggers, Views, and Schema Modification
An Refresher and How-To Profile Data using SQL
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Analytic Window Functions
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
OnContact CRM Customer Relationship Management
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
Lecture#7: Fun with SQL (Part 2)
Using SQL to Prepare Data for Analysis
SQL Server Analysis Services Fundamentals
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
Enhance BI Applications and Simplify Development
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Oracle8i Analytical SQL Features
SQL – Entire Select.
Chapter 4 Summary Query.
T-SQL Window function deep dive part 2
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
DataMart (Data Warehouse) Tool:
Structured Query Language
Introduction to Window Functions
Data Analysis with SQL Window Functions
Introduction to T-sql Window functionS
SQL Aggregation.
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.
Slides based on those originally by : Parminder Jeet Kaur
Shelly Cashman: Microsoft Access 2016
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, 12+ years First sql Saturday event, and first time presenting 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 Window functions is singular Windows is an operating system, this is a window you’re opening a window into your result set Adam McDonald Senior Application / SQL Developer Smith Travel Research adam@str.com @adam_mcd1

References: High-Performance T-SQL Using Window Functions By: Itzik Ben-Gan Todays presentation is available on dropbox. Slides : https://goo.gl/x7IsBk Demo : https://goo.gl/2aGcsr

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 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 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 AVG Distribution / Analytic CUME_DIST FIRST_VALUE LAST_VALUE LAG LEAD PERCENTILE_CONT PERCENTILE_DISC PERCENT_RANK 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 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 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://goo.gl/x7IsBk Demo : https://goo.gl/2aGcsr Adam McDonald Senior Application / SQL Developer Smith Travel Research adam@str.com @adam_mcd1