ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Windowing
Functions in
SQL Server
Andrej Zafka
MS SQL Developer
12/06/2017
What are good for?
? Windowing functions are functions applied to sets of rows defined by a
clause called OVER().
? Used mainly for analytical purposes allowing to calculate:
1) Aggregates with details
2) De-duplicating data
3) Paging
4) Running Totals
5) Gaps and islands in data
? The idea behind this concept is to allow you to apply various calculations to
a set, or window, of rows and return a single value.
12/06/2017 ºÝºÝߣ 2/11
History and background of Windowing
Functions
? First time available from SQL Server 2005, still improving in SQL Server
2017
? Definition:
? Window is the set of rows, or relation, given as input to the logical query
processing phase where the window function appears.
? OVER() function defines for each row in result set of the query separate,
independent window
? This is true for all rows in the result set of the query. In other words, with respect
to each row, the OVER clause defines a window independent of the other rows.
? Strong math background:
By a ¡°set¡± we mean any collection M into a whole of definite, distinct objects m
(which are called the ¡°elements¡± of M) of our perception or of our thought.
¡ªJoseph W. Dauben, Georg Cantor (Princeton University Press, 1990)
12/06/2017 ºÝºÝߣ 3/11
1) Aggregates with details:
? SUM, AVG, MIN, MAX, COUNT functions return aggregates with GROUP BY clause
? But we are losing detail ¨C how to calculate % from total by category, or
difference between total and current row
? Subqueries ¨C heavy for IO and CPU
? Using CTE (Common Table Expressions) less painful, still ugly
? Windowing functions ¨C OVER() ¨C easy to read, easy to IO and CPU
? SQL Server¡¯s optimizer was coded with the logic to look for multiple functions with the
same window specification. If any are found, SQL Server will use the same visit
(whichever kind of scan was chosen) to the data for those.
12/06/2017 ºÝºÝߣ 4/11
2) De-duplicating data:
? Finding duplicates in table
? Common ETL issue
? Use ROW_NUMBER() per attribute, like OrderId, ProductId
? ¡°per attribute¡± means PARTITION BY
? Every other row within partition will have number +1
? Outside select * with condition Row = 1 will take ordered unique rows
12/06/2017 ºÝºÝߣ 5/11
3) Paging:
? Returns specific page with dynamic size of page
? Inputs are ¡°Page number¡± and ¡°Page size¡±
? Result are tens of rows from all records
? Common task to return just previous / current / next page
? We don¡¯t want to load all records, send them to application and then manipulate
/ filter / sort
12/06/2017 ºÝºÝߣ 6/11
4) Running Totals:
? Keep accumulating the values in one attribute (the aggregated element)
based on ordering defined by another attribute or attributes
? There are many examples in life for calculating running totals like:
? Calculating bank account balances
? Tracking product stock levels in a warehouse
? Tracking cumulative sales values
? Prior to SQL Server 2012, the set-based solutions used to calculate running
totals were extremely expensive ( Subtree cost fall from 43,5 to 0,09 with
windowing functions)
12/06/2017 ºÝºÝߣ 7/11
5) Gaps and Islands in data:
? The gaps problem involves identifying the ranges of missing values in the
sequence:
? Input (2),(3),(7),(8),(9),(11),(15),(16),(17),(28)
? Result are ranges: 4-6, 10, 12-14, 18-27
? Using LAG and LEAD functions
? The islands problem involves identifying ranges of existing values in the numeric
sequence.
? Result are ranges: 2-3, 7-9, 11, 15-17, 28
12/06/2017 ºÝºÝߣ 8/11
Windowing Functions ¨C what to take home
? OVER() looks tricky but is worth of additional effort
? WFs are standard across DB providers
? WF are usually faster than other approaches
? Use POC index (Partitioning, Ordering, Covering)
? SUM( Qty ) OVER ( PARTITION BY [ShipperId] ORDER BY OrderYearMonth )
? Partitioning: [ShipperId]
? Ordering: [OrderYearMonth]
? Covering: [Qty]
? CREATE INDEX Idx_ShipperId_OrderYearMonth_i_Qty ON Table ( ShipperId
/* P */, OrderYearMonth /* O */) INCLUDE(Qty /* C */);
12/06/2017 ºÝºÝߣ 9/11
Sources:
https://www.simple-talk.com/sql/learn-sql-server/window-functions-in-sql-
server/
http://www.sqlpassion.at/archive/2015/01/22/sql-server-windowing-functions-
rows-vs-range/
Microsoft? SQL Server ? 2012 High-Performance T-SQL Using Window
Functions
Itzik Ben-Gan
12/06/2017 ºÝºÝߣ 10/11
Thanks for your attention!
By ¡°Slovak SQL Server & BI User Group¡±
slovak@sqlpass.org
12/06/2017 ºÝºÝߣ 11/11
Please let me feedback on
SurveyMonkey:

More Related Content

What's hot (12)

U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)
Michael Rys
?
Sql server introduction
Sql server introductionSql server introduction
Sql server introduction
Riteshkiit
?
U-SQL User-Defined Operators (UDOs) (SQLBits 2016)
U-SQL User-Defined Operators (UDOs) (SQLBits 2016)U-SQL User-Defined Operators (UDOs) (SQLBits 2016)
U-SQL User-Defined Operators (UDOs) (SQLBits 2016)
Michael Rys
?
Statistics
StatisticsStatistics
Statistics
Riteshkiit
?
Saurabh_Patel_An Alternative way to Import Multiple Excel files with Multiple...
Saurabh_Patel_An Alternative way to Import Multiple Excel files with Multiple...Saurabh_Patel_An Alternative way to Import Multiple Excel files with Multiple...
Saurabh_Patel_An Alternative way to Import Multiple Excel files with Multiple...
Saurabh Patel
?
Sql server 2016: System Databases, data types, DML, json, and built-in functions
Sql server 2016: System Databases, data types, DML, json, and built-in functionsSql server 2016: System Databases, data types, DML, json, and built-in functions
Sql server 2016: System Databases, data types, DML, json, and built-in functions
Seyed Ibrahim
?
U-SQL Meta Data Catalog (SQLBits 2016)
U-SQL Meta Data Catalog (SQLBits 2016)U-SQL Meta Data Catalog (SQLBits 2016)
U-SQL Meta Data Catalog (SQLBits 2016)
Michael Rys
?
NoSQL & JSON
NoSQL & JSONNoSQL & JSON
NoSQL & JSON
Tien-Yang (Aiden) Wu
?
¡°N1QL¡± a Rich Query Language for Couchbase: Presented by Don Pinto, Couchbase
¡°N1QL¡± a Rich Query Language for Couchbase: Presented by Don Pinto, Couchbase¡°N1QL¡± a Rich Query Language for Couchbase: Presented by Don Pinto, Couchbase
¡°N1QL¡± a Rich Query Language for Couchbase: Presented by Don Pinto, Couchbase
Lucidworks
?
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with Epsilon
Sina Madani
?
XML Pipelines
XML PipelinesXML Pipelines
XML Pipelines
Stylus Studio
?
Columnstore indexes in sql server 2014
Columnstore indexes in sql server 2014Columnstore indexes in sql server 2014
Columnstore indexes in sql server 2014
Antonios Chatzipavlis
?
U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)
Michael Rys
?
Sql server introduction
Sql server introductionSql server introduction
Sql server introduction
Riteshkiit
?
U-SQL User-Defined Operators (UDOs) (SQLBits 2016)
U-SQL User-Defined Operators (UDOs) (SQLBits 2016)U-SQL User-Defined Operators (UDOs) (SQLBits 2016)
U-SQL User-Defined Operators (UDOs) (SQLBits 2016)
Michael Rys
?
Saurabh_Patel_An Alternative way to Import Multiple Excel files with Multiple...
Saurabh_Patel_An Alternative way to Import Multiple Excel files with Multiple...Saurabh_Patel_An Alternative way to Import Multiple Excel files with Multiple...
Saurabh_Patel_An Alternative way to Import Multiple Excel files with Multiple...
Saurabh Patel
?
Sql server 2016: System Databases, data types, DML, json, and built-in functions
Sql server 2016: System Databases, data types, DML, json, and built-in functionsSql server 2016: System Databases, data types, DML, json, and built-in functions
Sql server 2016: System Databases, data types, DML, json, and built-in functions
Seyed Ibrahim
?
U-SQL Meta Data Catalog (SQLBits 2016)
U-SQL Meta Data Catalog (SQLBits 2016)U-SQL Meta Data Catalog (SQLBits 2016)
U-SQL Meta Data Catalog (SQLBits 2016)
Michael Rys
?
¡°N1QL¡± a Rich Query Language for Couchbase: Presented by Don Pinto, Couchbase
¡°N1QL¡± a Rich Query Language for Couchbase: Presented by Don Pinto, Couchbase¡°N1QL¡± a Rich Query Language for Couchbase: Presented by Don Pinto, Couchbase
¡°N1QL¡± a Rich Query Language for Couchbase: Presented by Don Pinto, Couchbase
Lucidworks
?
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with Epsilon
Sina Madani
?

Similar to Windowing functions session for Slovak SQL Pass & BI (20)

SPL_ALL_EN.pptx
SPL_ALL_EN.pptxSPL_ALL_EN.pptx
SPL_ALL_EN.pptx
Õþºê ÕÅ
?
Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...
Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...
Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...
Michael Rys
?
Modernizing your database with SQL Server 2019
Modernizing your database with SQL Server 2019Modernizing your database with SQL Server 2019
Modernizing your database with SQL Server 2019
Antonios Chatzipavlis
?
An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1
Navneet Upneja
?
Oracle Query Optimizer - An Introduction
Oracle Query Optimizer - An IntroductionOracle Query Optimizer - An Introduction
Oracle Query Optimizer - An Introduction
adryanbub
?
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008
paulguerin
?
Sql server introduction fundamental
Sql server introduction fundamentalSql server introduction fundamental
Sql server introduction fundamental
Riteshkiit
?
Ms sql server architecture
Ms sql server architectureMs sql server architecture
Ms sql server architecture
Ajeet Singh
?
What's new in SQL Server Integration Services 2012?
What's new in SQL Server Integration Services 2012?What's new in SQL Server Integration Services 2012?
What's new in SQL Server Integration Services 2012?
Microsoft TechNet - Belgium and Luxembourg
?
U-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for DevelopersU-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for Developers
Michael Rys
?
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
Kulbir4
?
OracleStore: A Highly Performant RawStore Implementation for Hive Metastore
OracleStore: A Highly Performant RawStore Implementation for Hive MetastoreOracleStore: A Highly Performant RawStore Implementation for Hive Metastore
OracleStore: A Highly Performant RawStore Implementation for Hive Metastore
DataWorks Summit
?
NewSQL - Deliverance from BASE and back to SQL and ACID
NewSQL - Deliverance from BASE and back to SQL and ACIDNewSQL - Deliverance from BASE and back to SQL and ACID
NewSQL - Deliverance from BASE and back to SQL and ACID
Tony Rogerson
?
introductionofssis-130418034853-phpapp01.pptx
introductionofssis-130418034853-phpapp01.pptxintroductionofssis-130418034853-phpapp01.pptx
introductionofssis-130418034853-phpapp01.pptx
YashaswiniSrinivasan1
?
3 CityNetConf - sql+c#=u-sql
3 CityNetConf - sql+c#=u-sql3 CityNetConf - sql+c#=u-sql
3 CityNetConf - sql+c#=u-sql
?ukasz Grala
?
Introducing U-SQL (SQLPASS 2016)
Introducing U-SQL (SQLPASS 2016)Introducing U-SQL (SQLPASS 2016)
Introducing U-SQL (SQLPASS 2016)
Michael Rys
?
Conquering "big data": An introduction to shard query
Conquering "big data": An introduction to shard queryConquering "big data": An introduction to shard query
Conquering "big data": An introduction to shard query
Justin Swanhart
?
Sage 300 ERP: Technical Tour of Diagnostic Tools
Sage 300 ERP: Technical Tour of Diagnostic ToolsSage 300 ERP: Technical Tour of Diagnostic Tools
Sage 300 ERP: Technical Tour of Diagnostic Tools
Sage 300 ERP CS
?
SQL for Analytics.pdfSQL for Analytics.pdf
SQL for Analytics.pdfSQL for Analytics.pdfSQL for Analytics.pdfSQL for Analytics.pdf
SQL for Analytics.pdfSQL for Analytics.pdf
namtunguyen6
?
Azure Data Lake Analytics Deep Dive
Azure Data Lake Analytics Deep DiveAzure Data Lake Analytics Deep Dive
Azure Data Lake Analytics Deep Dive
Ilyas F ???
?
Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...
Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...
Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...
Michael Rys
?
Modernizing your database with SQL Server 2019
Modernizing your database with SQL Server 2019Modernizing your database with SQL Server 2019
Modernizing your database with SQL Server 2019
Antonios Chatzipavlis
?
An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1
Navneet Upneja
?
Oracle Query Optimizer - An Introduction
Oracle Query Optimizer - An IntroductionOracle Query Optimizer - An Introduction
Oracle Query Optimizer - An Introduction
adryanbub
?
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008
paulguerin
?
Sql server introduction fundamental
Sql server introduction fundamentalSql server introduction fundamental
Sql server introduction fundamental
Riteshkiit
?
Ms sql server architecture
Ms sql server architectureMs sql server architecture
Ms sql server architecture
Ajeet Singh
?
U-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for DevelopersU-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for Developers
Michael Rys
?
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
Kulbir4
?
OracleStore: A Highly Performant RawStore Implementation for Hive Metastore
OracleStore: A Highly Performant RawStore Implementation for Hive MetastoreOracleStore: A Highly Performant RawStore Implementation for Hive Metastore
OracleStore: A Highly Performant RawStore Implementation for Hive Metastore
DataWorks Summit
?
NewSQL - Deliverance from BASE and back to SQL and ACID
NewSQL - Deliverance from BASE and back to SQL and ACIDNewSQL - Deliverance from BASE and back to SQL and ACID
NewSQL - Deliverance from BASE and back to SQL and ACID
Tony Rogerson
?
introductionofssis-130418034853-phpapp01.pptx
introductionofssis-130418034853-phpapp01.pptxintroductionofssis-130418034853-phpapp01.pptx
introductionofssis-130418034853-phpapp01.pptx
YashaswiniSrinivasan1
?
3 CityNetConf - sql+c#=u-sql
3 CityNetConf - sql+c#=u-sql3 CityNetConf - sql+c#=u-sql
3 CityNetConf - sql+c#=u-sql
?ukasz Grala
?
Introducing U-SQL (SQLPASS 2016)
Introducing U-SQL (SQLPASS 2016)Introducing U-SQL (SQLPASS 2016)
Introducing U-SQL (SQLPASS 2016)
Michael Rys
?
Conquering "big data": An introduction to shard query
Conquering "big data": An introduction to shard queryConquering "big data": An introduction to shard query
Conquering "big data": An introduction to shard query
Justin Swanhart
?
Sage 300 ERP: Technical Tour of Diagnostic Tools
Sage 300 ERP: Technical Tour of Diagnostic ToolsSage 300 ERP: Technical Tour of Diagnostic Tools
Sage 300 ERP: Technical Tour of Diagnostic Tools
Sage 300 ERP CS
?
SQL for Analytics.pdfSQL for Analytics.pdf
SQL for Analytics.pdfSQL for Analytics.pdfSQL for Analytics.pdfSQL for Analytics.pdf
SQL for Analytics.pdfSQL for Analytics.pdf
namtunguyen6
?
Azure Data Lake Analytics Deep Dive
Azure Data Lake Analytics Deep DiveAzure Data Lake Analytics Deep Dive
Azure Data Lake Analytics Deep Dive
Ilyas F ???
?

Recently uploaded (20)

Abhijn?a?nas?a?kuntalam Play by Kalidas Based on the translation by Arthur W....
Abhijn?a?nas?a?kuntalam Play by Kalidas Based on the translation by Arthur W....Abhijn?a?nas?a?kuntalam Play by Kalidas Based on the translation by Arthur W....
Abhijn?a?nas?a?kuntalam Play by Kalidas Based on the translation by Arthur W....
kiranprava2002
?
Turinton Insights - Enterprise Agentic AI Platform
Turinton Insights - Enterprise Agentic AI PlatformTurinton Insights - Enterprise Agentic AI Platform
Turinton Insights - Enterprise Agentic AI Platform
vikrant530668
?
2025-02-26_PwC_Global-Compliance-Study-2025 (1).pdf
2025-02-26_PwC_Global-Compliance-Study-2025 (1).pdf2025-02-26_PwC_Global-Compliance-Study-2025 (1).pdf
2025-02-26_PwC_Global-Compliance-Study-2025 (1).pdf
pbavila
?
STS-PRELIM-2025.pptxtyyfddjugggfssghghihf
STS-PRELIM-2025.pptxtyyfddjugggfssghghihfSTS-PRELIM-2025.pptxtyyfddjugggfssghghihf
STS-PRELIM-2025.pptxtyyfddjugggfssghghihf
TristanEvasco
?
Statistics for Management - standard deviation.pptx
Statistics for Management - standard deviation.pptxStatistics for Management - standard deviation.pptx
Statistics for Management - standard deviation.pptx
Jeya Sree
?
Reason To Switch to DNNDNNs excel in handling huge volumes of data (e.g., ima...
Reason To Switch to DNNDNNs excel in handling huge volumes of data (e.g., ima...Reason To Switch to DNNDNNs excel in handling huge volumes of data (e.g., ima...
Reason To Switch to DNNDNNs excel in handling huge volumes of data (e.g., ima...
SrideviPcSenthilkuma
?
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdhFOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
cshdhdhvfsbzdb
?
AI-vs-Data-Science-Unraveling-the-Tech-Landscape
AI-vs-Data-Science-Unraveling-the-Tech-LandscapeAI-vs-Data-Science-Unraveling-the-Tech-Landscape
AI-vs-Data-Science-Unraveling-the-Tech-Landscape
Ozias Rondon
?
The rise of AI Agents - Beyond Automation_ The Rise of AI Agents in Service ...
The rise of AI Agents -  Beyond Automation_ The Rise of AI Agents in Service ...The rise of AI Agents -  Beyond Automation_ The Rise of AI Agents in Service ...
The rise of AI Agents - Beyond Automation_ The Rise of AI Agents in Service ...
Yasen Lilov
?
GE-108-LESSON8.pptxbshsnsnsnsnsnsnnsnsnsnsnsbd
GE-108-LESSON8.pptxbshsnsnsnsnsnsnnsnsnsnsnsbdGE-108-LESSON8.pptxbshsnsnsnsnsnsnnsnsnsnsnsbd
GE-108-LESSON8.pptxbshsnsnsnsnsnsnnsnsnsnsnsbd
HarleySamboFavor
?
Construction Management full notes (15CV61).pdf
Construction Management full notes (15CV61).pdfConstruction Management full notes (15CV61).pdf
Construction Management full notes (15CV61).pdf
Ajaharuddin1
?
Introduction to Microsoft Power BI is a business analytics service
Introduction to Microsoft Power BI is a business analytics serviceIntroduction to Microsoft Power BI is a business analytics service
Introduction to Microsoft Power BI is a business analytics service
Kongu Engineering College, Perundurai, Erode
?
buiding web based land registration buiding web based land registration and m...
buiding web based land registration buiding web based land registration and m...buiding web based land registration buiding web based land registration and m...
buiding web based land registration buiding web based land registration and m...
habtamudele9
?
chap2_nnejjejehhehehhhhhhhhhehslides.ppt
chap2_nnejjejehhehehhhhhhhhhehslides.pptchap2_nnejjejehhehehhhhhhhhhehslides.ppt
chap2_nnejjejehhehehhhhhhhhhehslides.ppt
Nikhil620181
?
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptxHadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
MdTahammulNoor
?
20-NoSQLMongoDbiig data analytics hB.pdf
20-NoSQLMongoDbiig data analytics hB.pdf20-NoSQLMongoDbiig data analytics hB.pdf
20-NoSQLMongoDbiig data analytics hB.pdf
ssuser2d043c
?
DII-WS Training Manual with Links_V2.pdf
DII-WS Training Manual with Links_V2.pdfDII-WS Training Manual with Links_V2.pdf
DII-WS Training Manual with Links_V2.pdf
coolprince739
?
AI-Powered Contact Centre Virtual Assistant DS
AI-Powered Contact Centre Virtual Assistant DSAI-Powered Contact Centre Virtual Assistant DS
AI-Powered Contact Centre Virtual Assistant DS
Srinivasan N
?
Chat Bots - An Analytical study including Indian players
Chat Bots - An Analytical study including Indian playersChat Bots - An Analytical study including Indian players
Chat Bots - An Analytical study including Indian players
DR. Ram Kumar Pathak
?
Exploratory data analysis (EDA) is used by data scientists to analyze and inv...
Exploratory data analysis (EDA) is used by data scientists to analyze and inv...Exploratory data analysis (EDA) is used by data scientists to analyze and inv...
Exploratory data analysis (EDA) is used by data scientists to analyze and inv...
jimmy841199
?
Abhijn?a?nas?a?kuntalam Play by Kalidas Based on the translation by Arthur W....
Abhijn?a?nas?a?kuntalam Play by Kalidas Based on the translation by Arthur W....Abhijn?a?nas?a?kuntalam Play by Kalidas Based on the translation by Arthur W....
Abhijn?a?nas?a?kuntalam Play by Kalidas Based on the translation by Arthur W....
kiranprava2002
?
Turinton Insights - Enterprise Agentic AI Platform
Turinton Insights - Enterprise Agentic AI PlatformTurinton Insights - Enterprise Agentic AI Platform
Turinton Insights - Enterprise Agentic AI Platform
vikrant530668
?
2025-02-26_PwC_Global-Compliance-Study-2025 (1).pdf
2025-02-26_PwC_Global-Compliance-Study-2025 (1).pdf2025-02-26_PwC_Global-Compliance-Study-2025 (1).pdf
2025-02-26_PwC_Global-Compliance-Study-2025 (1).pdf
pbavila
?
STS-PRELIM-2025.pptxtyyfddjugggfssghghihf
STS-PRELIM-2025.pptxtyyfddjugggfssghghihfSTS-PRELIM-2025.pptxtyyfddjugggfssghghihf
STS-PRELIM-2025.pptxtyyfddjugggfssghghihf
TristanEvasco
?
Statistics for Management - standard deviation.pptx
Statistics for Management - standard deviation.pptxStatistics for Management - standard deviation.pptx
Statistics for Management - standard deviation.pptx
Jeya Sree
?
Reason To Switch to DNNDNNs excel in handling huge volumes of data (e.g., ima...
Reason To Switch to DNNDNNs excel in handling huge volumes of data (e.g., ima...Reason To Switch to DNNDNNs excel in handling huge volumes of data (e.g., ima...
Reason To Switch to DNNDNNs excel in handling huge volumes of data (e.g., ima...
SrideviPcSenthilkuma
?
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdhFOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
cshdhdhvfsbzdb
?
AI-vs-Data-Science-Unraveling-the-Tech-Landscape
AI-vs-Data-Science-Unraveling-the-Tech-LandscapeAI-vs-Data-Science-Unraveling-the-Tech-Landscape
AI-vs-Data-Science-Unraveling-the-Tech-Landscape
Ozias Rondon
?
The rise of AI Agents - Beyond Automation_ The Rise of AI Agents in Service ...
The rise of AI Agents -  Beyond Automation_ The Rise of AI Agents in Service ...The rise of AI Agents -  Beyond Automation_ The Rise of AI Agents in Service ...
The rise of AI Agents - Beyond Automation_ The Rise of AI Agents in Service ...
Yasen Lilov
?
GE-108-LESSON8.pptxbshsnsnsnsnsnsnnsnsnsnsnsbd
GE-108-LESSON8.pptxbshsnsnsnsnsnsnnsnsnsnsnsbdGE-108-LESSON8.pptxbshsnsnsnsnsnsnnsnsnsnsnsbd
GE-108-LESSON8.pptxbshsnsnsnsnsnsnnsnsnsnsnsbd
HarleySamboFavor
?
Construction Management full notes (15CV61).pdf
Construction Management full notes (15CV61).pdfConstruction Management full notes (15CV61).pdf
Construction Management full notes (15CV61).pdf
Ajaharuddin1
?
buiding web based land registration buiding web based land registration and m...
buiding web based land registration buiding web based land registration and m...buiding web based land registration buiding web based land registration and m...
buiding web based land registration buiding web based land registration and m...
habtamudele9
?
chap2_nnejjejehhehehhhhhhhhhehslides.ppt
chap2_nnejjejehhehehhhhhhhhhehslides.pptchap2_nnejjejehhehehhhhhhhhhehslides.ppt
chap2_nnejjejehhehehhhhhhhhhehslides.ppt
Nikhil620181
?
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptxHadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
MdTahammulNoor
?
20-NoSQLMongoDbiig data analytics hB.pdf
20-NoSQLMongoDbiig data analytics hB.pdf20-NoSQLMongoDbiig data analytics hB.pdf
20-NoSQLMongoDbiig data analytics hB.pdf
ssuser2d043c
?
DII-WS Training Manual with Links_V2.pdf
DII-WS Training Manual with Links_V2.pdfDII-WS Training Manual with Links_V2.pdf
DII-WS Training Manual with Links_V2.pdf
coolprince739
?
AI-Powered Contact Centre Virtual Assistant DS
AI-Powered Contact Centre Virtual Assistant DSAI-Powered Contact Centre Virtual Assistant DS
AI-Powered Contact Centre Virtual Assistant DS
Srinivasan N
?
Chat Bots - An Analytical study including Indian players
Chat Bots - An Analytical study including Indian playersChat Bots - An Analytical study including Indian players
Chat Bots - An Analytical study including Indian players
DR. Ram Kumar Pathak
?
Exploratory data analysis (EDA) is used by data scientists to analyze and inv...
Exploratory data analysis (EDA) is used by data scientists to analyze and inv...Exploratory data analysis (EDA) is used by data scientists to analyze and inv...
Exploratory data analysis (EDA) is used by data scientists to analyze and inv...
jimmy841199
?

Windowing functions session for Slovak SQL Pass & BI

  • 1. Windowing Functions in SQL Server Andrej Zafka MS SQL Developer 12/06/2017
  • 2. What are good for? ? Windowing functions are functions applied to sets of rows defined by a clause called OVER(). ? Used mainly for analytical purposes allowing to calculate: 1) Aggregates with details 2) De-duplicating data 3) Paging 4) Running Totals 5) Gaps and islands in data ? The idea behind this concept is to allow you to apply various calculations to a set, or window, of rows and return a single value. 12/06/2017 ºÝºÝߣ 2/11
  • 3. History and background of Windowing Functions ? First time available from SQL Server 2005, still improving in SQL Server 2017 ? Definition: ? Window is the set of rows, or relation, given as input to the logical query processing phase where the window function appears. ? OVER() function defines for each row in result set of the query separate, independent window ? This is true for all rows in the result set of the query. In other words, with respect to each row, the OVER clause defines a window independent of the other rows. ? Strong math background: By a ¡°set¡± we mean any collection M into a whole of definite, distinct objects m (which are called the ¡°elements¡± of M) of our perception or of our thought. ¡ªJoseph W. Dauben, Georg Cantor (Princeton University Press, 1990) 12/06/2017 ºÝºÝߣ 3/11
  • 4. 1) Aggregates with details: ? SUM, AVG, MIN, MAX, COUNT functions return aggregates with GROUP BY clause ? But we are losing detail ¨C how to calculate % from total by category, or difference between total and current row ? Subqueries ¨C heavy for IO and CPU ? Using CTE (Common Table Expressions) less painful, still ugly ? Windowing functions ¨C OVER() ¨C easy to read, easy to IO and CPU ? SQL Server¡¯s optimizer was coded with the logic to look for multiple functions with the same window specification. If any are found, SQL Server will use the same visit (whichever kind of scan was chosen) to the data for those. 12/06/2017 ºÝºÝߣ 4/11
  • 5. 2) De-duplicating data: ? Finding duplicates in table ? Common ETL issue ? Use ROW_NUMBER() per attribute, like OrderId, ProductId ? ¡°per attribute¡± means PARTITION BY ? Every other row within partition will have number +1 ? Outside select * with condition Row = 1 will take ordered unique rows 12/06/2017 ºÝºÝߣ 5/11
  • 6. 3) Paging: ? Returns specific page with dynamic size of page ? Inputs are ¡°Page number¡± and ¡°Page size¡± ? Result are tens of rows from all records ? Common task to return just previous / current / next page ? We don¡¯t want to load all records, send them to application and then manipulate / filter / sort 12/06/2017 ºÝºÝߣ 6/11
  • 7. 4) Running Totals: ? Keep accumulating the values in one attribute (the aggregated element) based on ordering defined by another attribute or attributes ? There are many examples in life for calculating running totals like: ? Calculating bank account balances ? Tracking product stock levels in a warehouse ? Tracking cumulative sales values ? Prior to SQL Server 2012, the set-based solutions used to calculate running totals were extremely expensive ( Subtree cost fall from 43,5 to 0,09 with windowing functions) 12/06/2017 ºÝºÝߣ 7/11
  • 8. 5) Gaps and Islands in data: ? The gaps problem involves identifying the ranges of missing values in the sequence: ? Input (2),(3),(7),(8),(9),(11),(15),(16),(17),(28) ? Result are ranges: 4-6, 10, 12-14, 18-27 ? Using LAG and LEAD functions ? The islands problem involves identifying ranges of existing values in the numeric sequence. ? Result are ranges: 2-3, 7-9, 11, 15-17, 28 12/06/2017 ºÝºÝߣ 8/11
  • 9. Windowing Functions ¨C what to take home ? OVER() looks tricky but is worth of additional effort ? WFs are standard across DB providers ? WF are usually faster than other approaches ? Use POC index (Partitioning, Ordering, Covering) ? SUM( Qty ) OVER ( PARTITION BY [ShipperId] ORDER BY OrderYearMonth ) ? Partitioning: [ShipperId] ? Ordering: [OrderYearMonth] ? Covering: [Qty] ? CREATE INDEX Idx_ShipperId_OrderYearMonth_i_Qty ON Table ( ShipperId /* P */, OrderYearMonth /* O */) INCLUDE(Qty /* C */); 12/06/2017 ºÝºÝߣ 9/11
  • 11. Thanks for your attention! By ¡°Slovak SQL Server & BI User Group¡± slovak@sqlpass.org 12/06/2017 ºÝºÝߣ 11/11 Please let me feedback on SurveyMonkey: