SQL Windowing
By Sandun Perera
Geveo Australasia
08th August 2012
Topics
• What is windowing?
• Window aggregate functions
• Set-Based vs. Iterative Programming
• Use of window functions
• String concatenation
• Ranking functions
• Common Table Expressions (CTE)
• Optimising ranking functions
• Creating a sequence
• Removing duplicate entries
• Pivoting
• Running totals
• What’s new with 2012?
• Benchmarks
What is windowing
• A window function is a function applied to a set of rows. A window
is the term standard SQL uses to describe the context for the
function to operate in. SQL uses a clause called OVER in which you
provide the window specification.
• Window functions are functions applied to sets of rows defined by
a clause called OVER. They are used mainly for analytical purposes
allowing you to calculate running totals, calculate moving averages,
identify gaps and islands in your data, and perform many other
computations. These functions are based on an amazingly profound
concept in standard SQL (which is both an ISO and ANSI standard)—
the concept of windowing. 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. Window functions can help to solve a wide
variety of querying tasks by helping you express set calculations
more easily, intuitively, and efficiently than ever before.
Window aggregate functions
• COUNT()
• SUM()
• AVG()
• MAX()
• MIN()
SELECT SalesOrderID, SalesOrderDetailID,
SUM(LineTotal) AS sum_product,
SUM(LineTotal) OVER(PARTITION BY SalesOrderID) AS sum_all
FROM Sales.SalesOrderDetail
GROUP BY SalesOrderID, SalesOrderDetailID, LineTotal
Set-Based vs. Iterative Programming
To get to the bottom of this, one first needs to
understand the foundations of T-SQL, and what the set-
based approach truly is. When you do, you realize that
the set-based approach is non intuitive for most people,
whereas the iterative approach is. It’s just the way our
brains are programmed, and I will try to clarify this
shortly. The gap between iterative and set-based thinking
is quite big. The gap can be closed, though it certainly
isn’t easy to do so. And this is where window functions
can play an important role; I find them to be a great tool
that can help bridge the gap between the two approaches
and allow a more gradual transition to set-based thinking.
Use of window functions
• Paging
• De-duplicating data
• Returning top n rows per group
• Computing running totals
• Performing operations on intervals such as packing intervals, and
calculating the maximum number of concurrent sessions
• Identifying gaps and islands
• Computing percentiles
• Computing the mode of the distribution
• Sorting hierarchies
• Pivoting
• Computing recency (newness)
String concatenation
• Among many alternative solutions in SQL
Server to achieve ordered string
concatenation, one of the more efficient
techniques is based on XML manipulation
using the FOR XML option with the PATH
mode. SELECT SUBSTRING((SELECT ',' + Name as [text()]
FROM Production.ProductCategory
FOR XML PATH('')),
2, 100) AS Names
Ranking functions
• ROW_NUMBER()
• RANK()
• DENSE_RANK()
• NTILE()
SELECT StateProvinceID, StateProvinceCode, CountryRegionCode,
RANK() OVER(ORDER BY CountryRegionCode) AS rnk_all,
RANK() OVER(PARTITION BY CountryRegionCode ORDER BY StateProvinceCode) AS rnk_cust
FROM Person.StateProvince
Common Table Expression (CTE)
• Set based programming
• Can perform self referencing
• Replaces temp table technique
WITH tblA AS
(
SELECT PC.*,
COUNT(PSC.ProductSubcategoryID)
OVER(PARTITION BY PC.ProductCategoryID) AS SubCategoryCount
FROM Production.ProductCategory PC
JOIN Production.ProductSubcategory PSC
ON PC.ProductCategoryID = PSC.ProductCategoryID
)
SELECT DISTINCT * FROM tblA
Optimising ranking functions
• Forward scan
SELECT actid, tranid, val,
ROW_NUMBER() OVER(PARTITION BY actid ORDER BY val) AS rownum
FROM dbo.Transactions;
• Create indexes against ordering columns
CREATE INDEX idx_actid_val_i_tranid
ON dbo.Transactions(actid /* P */, val /* O */)
INCLUDE(tranid /* C */);
SELECT actid, tranid, val,
ROW_NUMBER() OVER(PARTITION BY actid ORDER BY val) AS rownum
FROM dbo.Transactions;
The execution plan performs an
ordered scan of the index to
satisfy the ordering
requirement. With larger sets,
the difference can be greater.
SELECT actid, tranid, val,
ROW_NUMBER() OVER(PARTITION BY actid ORDER BY val DESC) AS rownum
FROM dbo.Transactions;
• Backward scan
SELECT actid, tranid, val,
ROW_NUMBER() OVER(PARTITION BY actid ORDER BY val DESC) AS rownum
FROM dbo.Transactions
ORDER BY actid DESC;
What’s even more curious is what happens if you add a presentation ORDER BY
clause that requests to order by the partitioning column in descending order.
Suddenly, the iterators that compute the window function are willing to
consume the partitioning values in descending order and can rely on index
ordering for this. So simply adding a presentation ORDER BY clause with actid
DESC to our query removes the need for a Sort iterator.
• Window order clause is mandatory, and SQL
Server doesn’t allow the ordering to be based on
a constant (i.e. ORDER BY NULL), but surprisingly,
when passing an expression based on a sub query
that returns a constant, SQL Server will accept it.
At the same time, the optimizer un-nests, or
expands the expression and realizes that the
ordering is the same for all rows. Therefore, it
removes the ordering requirement from the input
data.
SELECT actid, tranid, val,
ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum
FROM dbo.Transactions;
Creating a sequence
CREATE FUNCTION dbo.GetSequence (@low AS BIGINT, @high AS BIGINT)
RETURNS TABLE AS
RETURN
WITH
L0 AS (SELECT c FROM (VALUES(1),(1)) AS D(c)),
L1 AS (SELECT 1 AS c FROM L0 AS A CROSS JOIN L0 AS B),
L2 AS (SELECT 1 AS c FROM L1 AS A CROSS JOIN L1 AS B),
L3 AS (SELECT 1 AS c FROM L2 AS A CROSS JOIN L2 AS B),
L4 AS (SELECT 1 AS c FROM L3 AS A CROSS JOIN L3 AS B),
L5 AS (SELECT 1 AS c FROM L4 AS A CROSS JOIN L4 AS B),
Nums AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum
FROM L5)
SELECT @low + rownum - 1 AS n
FROM Nums
ORDER BY rownum
OFFSET 0 ROWS FETCH FIRST @high - @low + 1 ROWS ONLY;
GO
SELECT n FROM dbo.GetSequence(11, 20);
Not only numbers
DECLARE @start AS DATE = '20120201',
@end AS DATE = '20120212';
SELECT n, DATEADD(day, n, @start) AS dt
FROM dbo.GetSequence(0, DATEDIFF(day, @start, @end)) AS Nums;
dt
---------------
2012-02-01
2012-02-02
2012-02-03
…
2012-02-10
2012-02-11
2012-02-12
Removing duplicate entries
WITH C AS
(
SELECT orderid,
ROW_NUMBER() OVER(PARTITION BY orderid
ORDER BY (SELECT NULL)) AS n
FROM Sales.MyOrders
)
DELETE FROM C
WHERE n > 1;
Pivoting
• Pivoting is a technique to aggregate and rotate
data from a state of rows to columns.
• When pivoting data, you need to identify
three elements: the element you want to see
on rows (the grouping element), the element
you want to see on columns (the spreading
element), and the element you want to see in
the data portion (the aggregation element).
WITH C AS
(
SELECT YEAR(ModifiedDate) AS OrderYear,
MONTH(ModifiedDate) AS OrderMonth,
LineTotal as Amount
FROM Sales.SalesOrderDetail
)
SELECT *
FROM C
PIVOT(SUM(Amount)
FOR OrderMonth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS P;
• Suppose you need to query the Sales.OrderValues view and
return a row for each order year, a column for each order
month, and the sum of order values for each year and
month intersection. In this request, the on rows, or
grouping element is YEAR(orderdate); the on cols, or
spreading element is MONTH(orderdate); the distinct
spreading values are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12;
and the data, or aggregation element is SUM(val).
orderyear 1 2 3 4 5 6
---------- --------- --------- ---------- ----------- --------- ---------
2007 61258.08 38483.64 38547.23 53032.95 53781.30 36362.82
2008 94222.12 99415.29 104854.18 123798.70 18333.64 NULL
2006 NULL NULL NULL NULL NULL NULL
orderyear 7 8 9 10 11 12
---------- --------- --------- --------- --------- --------- ---------
2007 51020.86 47287.68 55629.27 66749.23 43533.80 71398.44
2008 NULL NULL NULL NULL NULL NULL
2006 27861.90 25485.28 26381.40 37515.73 45600.05 45239.63
WITH C AS
(
SELECT YEAR(orderdate) AS orderyear, MONTH(orderdate) AS ordermonth, val
FROM Sales.OrderValues
)
SELECT orderyear, (CAST([1] AS VARCHAR) + ‘, ‘ + CAST([2] AS VARCHAR)) AS Totals
FROM C
PIVOT(SUM(val)
FOR ordermonth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS P;
orderyear Totals
----------------------------------------
2000 11011.75, 10952.62
2001 10926.32, 10759.00
2002 10856.55, 10682.69
2003 11016.10, 10953.25
2004 10924.99, 10875.14
2005 11058.40, 10956.20
2006 10826.33, 10679.79
...
Running totals
• Prior to SQL Server 2012, the set-based
solutions used to calculate running totals were
extremely expensive. Therefore, people often
resorted to iterative solutions that weren’t
very fast but in certain data distribution
scenarios were faster than the set-based
solutions.
• Set based solution
• Cursor based solution
• CLR based solution
SELECT T1.ProductID, T1.TransactionID, T1.ActualCost,
SUM(T2.ActualCost) AS balance
FROM Production.TransactionHistory AS T1
JOIN Production.TransactionHistory AS T2
ON T2.ProductID = T1.ProductID
AND T2.TransactionID <= T1.TransactionID
GROUP BY T1.ProductID, T1.TransactionID, T1.ActualCost;
What’s new with 2012?
• Distribution functions are PERCENT_RANK,
CUME_DIST, PERCENTILE_CONT, and
PERCENTILE_DISC.
• Offset functions are LAG, LEAD, FIRST_VALUE,
LAST_VALUE, and NTH_VALUE. (There’s no support
for the NTH_VALUE function yet in SQL Server as of SQL
Server 2012.)
Benchmarks
SQL Windowing
References
• Microsoft® SQL Server® 2012 High-Performance
T-SQL Using Window Functions
– Itzik Ben-Gan
• Ranking functions
– http://sandunangelo.blogspot.com/2012/02/sql-
server-2008-ranking-functions.html
• Common Table Expressions
– http://sandunangelo.blogspot.com/2012/04/alternati
ve-for-temporary-tables-in.html
SQL Windowing

More Related Content

PDF
Sql server windowing functions
PDF
Window functions with SQL Server 2016
PPTX
Simplifying SQL with CTE's and windowing functions
PPTX
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...
PDF
Window functions for Data Science
PPTX
Mastering T-SQL Window Functions
PPTX
ZekeLabs PLSQL slides
PPTX
Sql Functions And Procedures
Sql server windowing functions
Window functions with SQL Server 2016
Simplifying SQL with CTE's and windowing functions
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...
Window functions for Data Science
Mastering T-SQL Window Functions
ZekeLabs PLSQL slides
Sql Functions And Procedures

What's hot (20)

ODP
SQL Tunning
ODP
Oracle SQL Advanced
PPTX
Query hierarchical data the easy way, with CTEs
PPT
Oracle Course
PPTX
Oracle basic queries
PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
PPTX
New T-SQL Features in SQL Server 2012
PDF
Introduction To Oracle Sql
PPTX
U-SQL Does SQL (SQLBits 2016)
PPTX
Oraclesql
PDF
Mssql to oracle
PDF
Oracle SQL Basics
PDF
TSQL Coding Guidelines
PDF
Structured Query Language (SQL) - An Introduction
PDF
Uncovering SQL Server query problems with execution plans - Tony Davis
PDF
Oracle Advanced SQL and Analytic Functions
PDF
Dare to build vertical design with relational data (Entity-Attribute-Value)
PPTX
Advanced functions in PL SQL
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
SQL Tunning
Oracle SQL Advanced
Query hierarchical data the easy way, with CTEs
Oracle Course
Oracle basic queries
Exploring Advanced SQL Techniques Using Analytic Functions
New T-SQL Features in SQL Server 2012
Introduction To Oracle Sql
U-SQL Does SQL (SQLBits 2016)
Oraclesql
Mssql to oracle
Oracle SQL Basics
TSQL Coding Guidelines
Structured Query Language (SQL) - An Introduction
Uncovering SQL Server query problems with execution plans - Tony Davis
Oracle Advanced SQL and Analytic Functions
Dare to build vertical design with relational data (Entity-Attribute-Value)
Advanced functions in PL SQL
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Exploring Advanced SQL Techniques Using Analytic Functions
Ad

Similar to SQL Windowing (20)

PDF
Data Love Conference - Window Functions for Database Analytics
PPTX
Windowing functions session for Slovak SQL Pass & BI
PDF
advance-sqaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal.pdf
PDF
Building advanced data-driven applications
PPSX
Analytic & Windowing functions in oracle
PDF
Windowing Functions - Little Rock Tech fest 2019
PDF
Windowing Functions - Little Rock Tech Fest 2019
PPTX
SQL techniques for faster applications
PDF
Oracle_Analytical_function.pdf
PDF
Window functions in MariaDB 10.2
PDF
KScope19 - SQL Features
PDF
Introducing Windowing Functions (pgCon 2009)
PPTX
Perth APAC Groundbreakers tour - SQL Techniques
PDF
You can do THAT without Perl?
PDF
UKOUG 2019 - SQL features
PDF
ILOUG 2019 - SQL features for Developers
PDF
T sql denali code Day of .Net
PPTX
Sangam 18 - Great Applications with Great SQL
PDF
Oracle12c For Developers
PDF
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
Data Love Conference - Window Functions for Database Analytics
Windowing functions session for Slovak SQL Pass & BI
advance-sqaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal.pdf
Building advanced data-driven applications
Analytic & Windowing functions in oracle
Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech Fest 2019
SQL techniques for faster applications
Oracle_Analytical_function.pdf
Window functions in MariaDB 10.2
KScope19 - SQL Features
Introducing Windowing Functions (pgCon 2009)
Perth APAC Groundbreakers tour - SQL Techniques
You can do THAT without Perl?
UKOUG 2019 - SQL features
ILOUG 2019 - SQL features for Developers
T sql denali code Day of .Net
Sangam 18 - Great Applications with Great SQL
Oracle12c For Developers
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
Ad

More from Sandun Perera (12)

PPTX
Azure EventGrid vs Azure ServiceBus.pptx
PPTX
iUpgradable
PPTX
Bootstrap 5 whats new
PPTX
PPTX
Angular Form Validations
PPTX
Introduction to NuoDB
PPTX
Microsoft Dynamics CRM 2013 Customization
PPTX
Windows PowerShell
PPTX
Car care
PPTX
Tale of the photo camera
PPTX
Visual Studio Unleashed - Tips and Tricks
PPTX
What’s new in Visual Studio 2010
Azure EventGrid vs Azure ServiceBus.pptx
iUpgradable
Bootstrap 5 whats new
Angular Form Validations
Introduction to NuoDB
Microsoft Dynamics CRM 2013 Customization
Windows PowerShell
Car care
Tale of the photo camera
Visual Studio Unleashed - Tips and Tricks
What’s new in Visual Studio 2010

Recently uploaded (20)

PPTX
MCP empowers AI Agents from Zero to Production
PPT
3.Software Design for software engineering
PDF
Top AI Tools for Project Managers: My 2025 AI Stack
PPTX
Greedy best-first search algorithm always selects the path which appears best...
PDF
Top 10 Project Management Software for Small Teams in 2025.pdf
PPTX
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
PPTX
StacksandQueuesCLASS 12 COMPUTER SCIENCE.pptx
PPTX
ROI from Efficient Content & Campaign Management in the Digital Media Industry
PDF
Multiverse AI Review 2025_ The Ultimate All-in-One AI Platform.pdf
PDF
WhatsApp Chatbots The Key to Scalable Customer Support.pdf
PPTX
Relevance Tuning with Genetic Algorithms
PDF
Adlice Diag Crack With Serial Key Free Download 2025
PDF
How to Set Realistic Project Milestones and Deadlines
PPTX
Improving Audience Engagement ROI with ERP-Powered Insights
PDF
What Makes a Great Data Visualization Consulting Service.pdf
PDF
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
PDF
IT Consulting Services to Secure Future Growth
PPTX
Comprehensive Guide to Digital Image Processing Concepts and Applications
PPTX
Independent Consultants’ Biggest Challenges in ERP Projects – and How Apagen ...
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
MCP empowers AI Agents from Zero to Production
3.Software Design for software engineering
Top AI Tools for Project Managers: My 2025 AI Stack
Greedy best-first search algorithm always selects the path which appears best...
Top 10 Project Management Software for Small Teams in 2025.pdf
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
StacksandQueuesCLASS 12 COMPUTER SCIENCE.pptx
ROI from Efficient Content & Campaign Management in the Digital Media Industry
Multiverse AI Review 2025_ The Ultimate All-in-One AI Platform.pdf
WhatsApp Chatbots The Key to Scalable Customer Support.pdf
Relevance Tuning with Genetic Algorithms
Adlice Diag Crack With Serial Key Free Download 2025
How to Set Realistic Project Milestones and Deadlines
Improving Audience Engagement ROI with ERP-Powered Insights
What Makes a Great Data Visualization Consulting Service.pdf
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
IT Consulting Services to Secure Future Growth
Comprehensive Guide to Digital Image Processing Concepts and Applications
Independent Consultants’ Biggest Challenges in ERP Projects – and How Apagen ...
Understanding the Need for Systemic Change in Open Source Through Intersectio...

SQL Windowing

  • 1. SQL Windowing By Sandun Perera Geveo Australasia 08th August 2012
  • 2. Topics • What is windowing? • Window aggregate functions • Set-Based vs. Iterative Programming • Use of window functions • String concatenation • Ranking functions • Common Table Expressions (CTE) • Optimising ranking functions • Creating a sequence • Removing duplicate entries • Pivoting • Running totals • What’s new with 2012? • Benchmarks
  • 3. What is windowing • A window function is a function applied to a set of rows. A window is the term standard SQL uses to describe the context for the function to operate in. SQL uses a clause called OVER in which you provide the window specification. • Window functions are functions applied to sets of rows defined by a clause called OVER. They are used mainly for analytical purposes allowing you to calculate running totals, calculate moving averages, identify gaps and islands in your data, and perform many other computations. These functions are based on an amazingly profound concept in standard SQL (which is both an ISO and ANSI standard)— the concept of windowing. 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. Window functions can help to solve a wide variety of querying tasks by helping you express set calculations more easily, intuitively, and efficiently than ever before.
  • 4. Window aggregate functions • COUNT() • SUM() • AVG() • MAX() • MIN() SELECT SalesOrderID, SalesOrderDetailID, SUM(LineTotal) AS sum_product, SUM(LineTotal) OVER(PARTITION BY SalesOrderID) AS sum_all FROM Sales.SalesOrderDetail GROUP BY SalesOrderID, SalesOrderDetailID, LineTotal
  • 5. Set-Based vs. Iterative Programming To get to the bottom of this, one first needs to understand the foundations of T-SQL, and what the set- based approach truly is. When you do, you realize that the set-based approach is non intuitive for most people, whereas the iterative approach is. It’s just the way our brains are programmed, and I will try to clarify this shortly. The gap between iterative and set-based thinking is quite big. The gap can be closed, though it certainly isn’t easy to do so. And this is where window functions can play an important role; I find them to be a great tool that can help bridge the gap between the two approaches and allow a more gradual transition to set-based thinking.
  • 6. Use of window functions • Paging • De-duplicating data • Returning top n rows per group • Computing running totals • Performing operations on intervals such as packing intervals, and calculating the maximum number of concurrent sessions • Identifying gaps and islands • Computing percentiles • Computing the mode of the distribution • Sorting hierarchies • Pivoting • Computing recency (newness)
  • 7. String concatenation • Among many alternative solutions in SQL Server to achieve ordered string concatenation, one of the more efficient techniques is based on XML manipulation using the FOR XML option with the PATH mode. SELECT SUBSTRING((SELECT ',' + Name as [text()] FROM Production.ProductCategory FOR XML PATH('')), 2, 100) AS Names
  • 8. Ranking functions • ROW_NUMBER() • RANK() • DENSE_RANK() • NTILE() SELECT StateProvinceID, StateProvinceCode, CountryRegionCode, RANK() OVER(ORDER BY CountryRegionCode) AS rnk_all, RANK() OVER(PARTITION BY CountryRegionCode ORDER BY StateProvinceCode) AS rnk_cust FROM Person.StateProvince
  • 9. Common Table Expression (CTE) • Set based programming • Can perform self referencing • Replaces temp table technique WITH tblA AS ( SELECT PC.*, COUNT(PSC.ProductSubcategoryID) OVER(PARTITION BY PC.ProductCategoryID) AS SubCategoryCount FROM Production.ProductCategory PC JOIN Production.ProductSubcategory PSC ON PC.ProductCategoryID = PSC.ProductCategoryID ) SELECT DISTINCT * FROM tblA
  • 10. Optimising ranking functions • Forward scan SELECT actid, tranid, val, ROW_NUMBER() OVER(PARTITION BY actid ORDER BY val) AS rownum FROM dbo.Transactions;
  • 11. • Create indexes against ordering columns CREATE INDEX idx_actid_val_i_tranid ON dbo.Transactions(actid /* P */, val /* O */) INCLUDE(tranid /* C */); SELECT actid, tranid, val, ROW_NUMBER() OVER(PARTITION BY actid ORDER BY val) AS rownum FROM dbo.Transactions; The execution plan performs an ordered scan of the index to satisfy the ordering requirement. With larger sets, the difference can be greater.
  • 12. SELECT actid, tranid, val, ROW_NUMBER() OVER(PARTITION BY actid ORDER BY val DESC) AS rownum FROM dbo.Transactions; • Backward scan
  • 13. SELECT actid, tranid, val, ROW_NUMBER() OVER(PARTITION BY actid ORDER BY val DESC) AS rownum FROM dbo.Transactions ORDER BY actid DESC; What’s even more curious is what happens if you add a presentation ORDER BY clause that requests to order by the partitioning column in descending order. Suddenly, the iterators that compute the window function are willing to consume the partitioning values in descending order and can rely on index ordering for this. So simply adding a presentation ORDER BY clause with actid DESC to our query removes the need for a Sort iterator.
  • 14. • Window order clause is mandatory, and SQL Server doesn’t allow the ordering to be based on a constant (i.e. ORDER BY NULL), but surprisingly, when passing an expression based on a sub query that returns a constant, SQL Server will accept it. At the same time, the optimizer un-nests, or expands the expression and realizes that the ordering is the same for all rows. Therefore, it removes the ordering requirement from the input data. SELECT actid, tranid, val, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum FROM dbo.Transactions;
  • 15. Creating a sequence CREATE FUNCTION dbo.GetSequence (@low AS BIGINT, @high AS BIGINT) RETURNS TABLE AS RETURN WITH L0 AS (SELECT c FROM (VALUES(1),(1)) AS D(c)), L1 AS (SELECT 1 AS c FROM L0 AS A CROSS JOIN L0 AS B), L2 AS (SELECT 1 AS c FROM L1 AS A CROSS JOIN L1 AS B), L3 AS (SELECT 1 AS c FROM L2 AS A CROSS JOIN L2 AS B), L4 AS (SELECT 1 AS c FROM L3 AS A CROSS JOIN L3 AS B), L5 AS (SELECT 1 AS c FROM L4 AS A CROSS JOIN L4 AS B), Nums AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum FROM L5) SELECT @low + rownum - 1 AS n FROM Nums ORDER BY rownum OFFSET 0 ROWS FETCH FIRST @high - @low + 1 ROWS ONLY; GO SELECT n FROM dbo.GetSequence(11, 20);
  • 16. Not only numbers DECLARE @start AS DATE = '20120201', @end AS DATE = '20120212'; SELECT n, DATEADD(day, n, @start) AS dt FROM dbo.GetSequence(0, DATEDIFF(day, @start, @end)) AS Nums; dt --------------- 2012-02-01 2012-02-02 2012-02-03 … 2012-02-10 2012-02-11 2012-02-12
  • 17. Removing duplicate entries WITH C AS ( SELECT orderid, ROW_NUMBER() OVER(PARTITION BY orderid ORDER BY (SELECT NULL)) AS n FROM Sales.MyOrders ) DELETE FROM C WHERE n > 1;
  • 18. Pivoting • Pivoting is a technique to aggregate and rotate data from a state of rows to columns. • When pivoting data, you need to identify three elements: the element you want to see on rows (the grouping element), the element you want to see on columns (the spreading element), and the element you want to see in the data portion (the aggregation element).
  • 19. WITH C AS ( SELECT YEAR(ModifiedDate) AS OrderYear, MONTH(ModifiedDate) AS OrderMonth, LineTotal as Amount FROM Sales.SalesOrderDetail ) SELECT * FROM C PIVOT(SUM(Amount) FOR OrderMonth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS P; • Suppose you need to query the Sales.OrderValues view and return a row for each order year, a column for each order month, and the sum of order values for each year and month intersection. In this request, the on rows, or grouping element is YEAR(orderdate); the on cols, or spreading element is MONTH(orderdate); the distinct spreading values are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12; and the data, or aggregation element is SUM(val).
  • 20. orderyear 1 2 3 4 5 6 ---------- --------- --------- ---------- ----------- --------- --------- 2007 61258.08 38483.64 38547.23 53032.95 53781.30 36362.82 2008 94222.12 99415.29 104854.18 123798.70 18333.64 NULL 2006 NULL NULL NULL NULL NULL NULL orderyear 7 8 9 10 11 12 ---------- --------- --------- --------- --------- --------- --------- 2007 51020.86 47287.68 55629.27 66749.23 43533.80 71398.44 2008 NULL NULL NULL NULL NULL NULL 2006 27861.90 25485.28 26381.40 37515.73 45600.05 45239.63
  • 21. WITH C AS ( SELECT YEAR(orderdate) AS orderyear, MONTH(orderdate) AS ordermonth, val FROM Sales.OrderValues ) SELECT orderyear, (CAST([1] AS VARCHAR) + ‘, ‘ + CAST([2] AS VARCHAR)) AS Totals FROM C PIVOT(SUM(val) FOR ordermonth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS P; orderyear Totals ---------------------------------------- 2000 11011.75, 10952.62 2001 10926.32, 10759.00 2002 10856.55, 10682.69 2003 11016.10, 10953.25 2004 10924.99, 10875.14 2005 11058.40, 10956.20 2006 10826.33, 10679.79 ...
  • 22. Running totals • Prior to SQL Server 2012, the set-based solutions used to calculate running totals were extremely expensive. Therefore, people often resorted to iterative solutions that weren’t very fast but in certain data distribution scenarios were faster than the set-based solutions.
  • 23. • Set based solution • Cursor based solution • CLR based solution SELECT T1.ProductID, T1.TransactionID, T1.ActualCost, SUM(T2.ActualCost) AS balance FROM Production.TransactionHistory AS T1 JOIN Production.TransactionHistory AS T2 ON T2.ProductID = T1.ProductID AND T2.TransactionID <= T1.TransactionID GROUP BY T1.ProductID, T1.TransactionID, T1.ActualCost;
  • 24. What’s new with 2012? • Distribution functions are PERCENT_RANK, CUME_DIST, PERCENTILE_CONT, and PERCENTILE_DISC. • Offset functions are LAG, LEAD, FIRST_VALUE, LAST_VALUE, and NTH_VALUE. (There’s no support for the NTH_VALUE function yet in SQL Server as of SQL Server 2012.)
  • 27. References • Microsoft® SQL Server® 2012 High-Performance T-SQL Using Window Functions – Itzik Ben-Gan • Ranking functions – http://sandunangelo.blogspot.com/2012/02/sql- server-2008-ranking-functions.html • Common Table Expressions – http://sandunangelo.blogspot.com/2012/04/alternati ve-for-temporary-tables-in.html