Writing Better Queries with Window Functions

Slides:



Advertisements
Similar presentations
Geographic Information Systems
Advertisements

Solving Problems in ETL using SSIS Allan Mitchell SQL Server MVP
CRA Administering Microsoft SQL Server 2012 Databases Implementing a Data Warehouse with Microsoft SQL Server = Querying Microsoft.
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.
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.
A Linear Regression Algorithm Using Windowing Functions KEVIN MCCARTY.
SPONSORS. Microsoft PowerPivot for SQL Server, Excel 2010, and SharePoint 2010 Michael Herman Syntergy, Inc.
What is SQL and Who uses it? Presented by: John Deardurff Global McOWL Internal Sales Training October 24, 2014.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
+ Administering Microsoft SQL Server 2012 Databases Implementing a Data Warehouse with Microsoft SQL Server = Querying Microsoft SQL.
+ Administering Microsoft SQL Server 2012 Databases Implementing a Data Warehouse with Microsoft SQL Server = Querying Microsoft SQL.
CRA Administering Microsoft SQL Server 2012 Databases Implementing a Data Warehouse with Microsoft SQL Server = Querying Microsoft.
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.
Modern Data Warehouse: Microsoft APS Alain Dormehl June 2015.
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.
Welcome José Grilo Server and Tools Lead Microsoft Portugal
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.
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.
Microsoft SQL Server Database & Business Intelligence Training Training/Placement/Certification Classroom/Online/Corporate Fast track/Regular/Weekend Contact.
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
Max Fritz Senior Systems Consultant, Now Micro
Segmap Solutions Mapping segments.
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
Window function performance
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Mission-critical performance with Microsoft SQL Server 2016
Using Window Ranking, Offset, and Aggregate Functions
What is business intelligence?
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
Buy Valid Microsoft Exam Study Guide Dumps Questions Answers Realexamdumps.com
 Microsoft owned SQL server is an SQL based Relational DBMS which offers corporate solutions to database management. SQL Server Optimization can be a.
WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan
T-SQL Window Functions in Microsoft SQL Server Denali
WINDOW FUNCTIONS ARE YOUR FRIENDS Dejan
T-SQL Power! The OVER Clause: Your Key to No-Sweat Problem Solving
T-SQL Window Function Deep Dive part 1
Designing Business Intelligence Solutions with Microsoft SQL Server
Jan Engelsberg Program Manager, SQL Server
Oracle8i Analytical SQL Features
Delivering an End-to-End Business Intelligence Solution
T-SQL Window function deep dive part 2
Transact SQL Performance Tips
Introduction to Window Functions
T-SQL gotchas and power-ups
Data Analysis with SQL Window Functions
Introduction to T-sql Window functionS
Mary Ledbetter, Systems Sales Engineer
Why Innovate with Lagom & SAP?
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.
Implementing Data Models & Reports with Microsoft SQL Server
Intermediate Query Structure and Development
T-SQL: Simple Changes That Go a Long Way
Presentation transcript:

Writing Better Queries with Window Functions Kathi Kellenberger, MVP Linchpin People auntkathisql.com

History 2005: Window functions introduced 2012: Window functions enhanced

Agenda What are window functions? Ranking functions Window aggregates 2012 Enhancements: ORDER BY 2012 Enhancements: Framing Eight new functions Making Business Intelligent

What are window functions? 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

ROW_NUMBER() example CustomerID OrderID Total Row_Number() Over(Order by OrderID) 1 101 100 2 102 40 103 11 3 104 432 4 105 2000 5 106 300 6 107 674 7 108 76 8 109 234 9 110 889 10

ROW_NUMBER() example CustomerID OrderID TotalDue Row_Number() Over(Partition by CustomerID Order by OrderID) 1 100 5 2000 2 6 300 3 40 11 4 432 7 674 9 234 10 889 8 76

Ranking Functions Demo

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 2005 functionality NOT optimized for performance

Window aggregate example CustomerID OrderID TotalDue Sum(TotalDue) Over(Partition by CustomerID) 1 100 2400 5 2000 6 300 2 40 51 3 11 4 432 7 674 1797 9 234 10 889 8 76 310

Window Aggregates Demo

2012 enhancements ORDER BY clause added to window aggregates Even further define the window with ROWS and RANGE: FRAMING Eight new functions

Window aggregate ORDER BY example CustomerID OrderID TotalDue Sum(TotalDue) Over(Order by OrderID) 1 100 2 40 140 3 11 151 4 432 583 5 2000 2583 6 300 2883 7 674 3557 8 76 3633 9 234 3867 10 889 4756 4990

Running Total Demo

FRAME: ROWS and RANGE Further define window Terms UNBOUNDED PRECEDING UNBOUNDED FOLLOWING CURRENT ROW # PRECEDING # FOLLOWING RANGE is not fully implemented and can hurt performance

Framing Rows between unbounded preceding and current row

Framing Rows between current row and unbounded following

Framing Rows between 2 preceding and current row

Framing Rows between 5 preceding and 2 following

Framing Demo

Cultivating a Healthy Database Office Hours June 24 2015 at 12:00 ET http://tiny.cc/dkb9yx Text LINCHPIN to 33444 Linchpin People is a team of amazing experts focused entirely on the Microsoft database ecosystem Linchpin People is comprised of a team of world class SQL Server Experts and MVPs who work together to create and deliver top-of-the-line Microsoft Data Ecosystem solutions. We can help you design new projects that are on the drawing board, Or we can rescue projects that are already being implemented but may need some expert assistance to successfully meet their intended goals. Our expertise spans the Microsoft ecosystem - from SQL Server based performance tuning, high availability and disaster recovery, data integration architecture, ETL and data warehouse consulting, big data, complex data, business intelligence and analytics, to platform agnostic application development with a data-centric focus. Have a problem in this space? We can help.

The New 2012 Functions LAG() and LEAD() Look back or look forward FIRST_VALUE() and LAST_VALUE() The very first or very last PERCENT_RANK() and CUME_DIST() Calculate ranking PERCENTILE_DISC() and PERCENTILE_CONT() Given a percent, find the value

Analytic Functions Demo