SlideShare a Scribd company logo
1 of 28
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
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

What's hot

Query hierarchical data the easy way, with CTEs
Query hierarchical data the easy way, with CTEsQuery hierarchical data the easy way, with CTEs
Query hierarchical data the easy way, with CTEsMariaDB plc
 
Oracle Course
Oracle CourseOracle Course
Oracle Courserspaike
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queriesPRAKHAR JHA
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsZohar Elkayam
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 Richie Rump
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle SqlAhmed Yaseen
 
U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)Michael Rys
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding GuidelinesChris Adkin
 
Structured Query Language (SQL) - An Introduction
Structured Query Language (SQL) - An IntroductionStructured Query Language (SQL) - An Introduction
Structured Query Language (SQL) - An IntroductionRajeev Srivastava
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisUncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisRed Gate Software
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsZohar Elkayam
 
Dare to build vertical design with relational data (Entity-Attribute-Value)
Dare to build vertical design with relational data (Entity-Attribute-Value)Dare to build vertical design with relational data (Entity-Attribute-Value)
Dare to build vertical design with relational data (Entity-Attribute-Value)Ivo Andreev
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQLHosein Zare
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsZohar Elkayam
 

What's hot (20)

SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Oracle SQL Advanced
Oracle SQL AdvancedOracle SQL Advanced
Oracle SQL Advanced
 
Query hierarchical data the easy way, with CTEs
Query hierarchical data the easy way, with CTEsQuery hierarchical data the easy way, with CTEs
Query hierarchical data the easy way, with CTEs
 
Oracle Course
Oracle CourseOracle Course
Oracle Course
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queries
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 
U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Mssql to oracle
Mssql to oracleMssql to oracle
Mssql to oracle
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
 
Structured Query Language (SQL) - An Introduction
Structured Query Language (SQL) - An IntroductionStructured Query Language (SQL) - An Introduction
Structured Query Language (SQL) - An Introduction
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisUncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony Davis
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic Functions
 
Dare to build vertical design with relational data (Entity-Attribute-Value)
Dare to build vertical design with relational data (Entity-Attribute-Value)Dare to build vertical design with relational data (Entity-Attribute-Value)
Dare to build vertical design with relational data (Entity-Attribute-Value)
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQL
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
 

Similar to SQL Windowing

Scaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQLScaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQLJim Mlodgenski
 
esProc introduction
esProc introductionesProc introduction
esProc introductionssuser9671cc
 
Spark Structured APIs
Spark Structured APIsSpark Structured APIs
Spark Structured APIsKnoldus Inc.
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdfKalyankumarVenkat1
 
Advanced SQL For Data Scientists
Advanced SQL For Data ScientistsAdvanced SQL For Data Scientists
Advanced SQL For Data ScientistsDatabricks
 
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sadDevelopers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sadmCloud
 
Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statementsxKinAnx
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Serban Tanasa
 
Developers’ mDay 2019. - Bogdan Kecman, Oracle – MySQL 8.0 – why upgrade
Developers’ mDay 2019. - Bogdan Kecman, Oracle – MySQL 8.0 – why upgradeDevelopers’ mDay 2019. - Bogdan Kecman, Oracle – MySQL 8.0 – why upgrade
Developers’ mDay 2019. - Bogdan Kecman, Oracle – MySQL 8.0 – why upgrademCloud
 
Apache Calcite: One Frontend to Rule Them All
Apache Calcite: One Frontend to Rule Them AllApache Calcite: One Frontend to Rule Them All
Apache Calcite: One Frontend to Rule Them AllMichael Mior
 
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1MariaDB plc
 
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1MariaDB plc
 
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionStar Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionFranck Pachot
 
cPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven FeaturescPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven FeaturesDave Stokes
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricksYanli Liu
 
Best Practices for Supercharging Cloud Analytics on Amazon Redshift
Best Practices for Supercharging Cloud Analytics on Amazon RedshiftBest Practices for Supercharging Cloud Analytics on Amazon Redshift
Best Practices for Supercharging Cloud Analytics on Amazon RedshiftSnapLogic
 
Managing Statistics for Optimal Query Performance
Managing Statistics for Optimal Query PerformanceManaging Statistics for Optimal Query Performance
Managing Statistics for Optimal Query PerformanceKaren Morton
 
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...Dave Stokes
 

Similar to SQL Windowing (20)

Scaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQLScaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQL
 
esProc introduction
esProc introductionesProc introduction
esProc introduction
 
Spark Structured APIs
Spark Structured APIsSpark Structured APIs
Spark Structured APIs
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 
Advanced SQL For Data Scientists
Advanced SQL For Data ScientistsAdvanced SQL For Data Scientists
Advanced SQL For Data Scientists
 
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sadDevelopers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
 
Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statements
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
 
Developers’ mDay 2019. - Bogdan Kecman, Oracle – MySQL 8.0 – why upgrade
Developers’ mDay 2019. - Bogdan Kecman, Oracle – MySQL 8.0 – why upgradeDevelopers’ mDay 2019. - Bogdan Kecman, Oracle – MySQL 8.0 – why upgrade
Developers’ mDay 2019. - Bogdan Kecman, Oracle – MySQL 8.0 – why upgrade
 
Apache Calcite: One Frontend to Rule Them All
Apache Calcite: One Frontend to Rule Them AllApache Calcite: One Frontend to Rule Them All
Apache Calcite: One Frontend to Rule Them All
 
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
 
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
 
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionStar Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
 
cPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven FeaturescPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven Features
 
Couchbas for dummies
Couchbas for dummiesCouchbas for dummies
Couchbas for dummies
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Best Practices for Supercharging Cloud Analytics on Amazon Redshift
Best Practices for Supercharging Cloud Analytics on Amazon RedshiftBest Practices for Supercharging Cloud Analytics on Amazon Redshift
Best Practices for Supercharging Cloud Analytics on Amazon Redshift
 
Managing Statistics for Optimal Query Performance
Managing Statistics for Optimal Query PerformanceManaging Statistics for Optimal Query Performance
Managing Statistics for Optimal Query Performance
 
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
 
PL/SQL and radix tree structure
PL/SQL and radix tree structurePL/SQL and radix tree structure
PL/SQL and radix tree structure
 

More from Sandun Perera

Azure EventGrid vs Azure ServiceBus.pptx
Azure EventGrid vs Azure ServiceBus.pptxAzure EventGrid vs Azure ServiceBus.pptx
Azure EventGrid vs Azure ServiceBus.pptxSandun Perera
 
Bootstrap 5 whats new
Bootstrap 5   whats newBootstrap 5   whats new
Bootstrap 5 whats newSandun Perera
 
Angular Form Validations
Angular Form ValidationsAngular Form Validations
Angular Form ValidationsSandun Perera
 
Introduction to NuoDB
Introduction to NuoDBIntroduction to NuoDB
Introduction to NuoDBSandun Perera
 
Microsoft Dynamics CRM 2013 Customization
Microsoft Dynamics CRM 2013 CustomizationMicrosoft Dynamics CRM 2013 Customization
Microsoft Dynamics CRM 2013 CustomizationSandun Perera
 
Tale of the photo camera
Tale of the photo cameraTale of the photo camera
Tale of the photo cameraSandun Perera
 
Visual Studio Unleashed - Tips and Tricks
Visual Studio Unleashed - Tips and TricksVisual Studio Unleashed - Tips and Tricks
Visual Studio Unleashed - Tips and TricksSandun Perera
 
What’s new in Visual Studio 2010
What’s new in Visual Studio 2010What’s new in Visual Studio 2010
What’s new in Visual Studio 2010Sandun Perera
 

More from Sandun Perera (12)

Azure EventGrid vs Azure ServiceBus.pptx
Azure EventGrid vs Azure ServiceBus.pptxAzure EventGrid vs Azure ServiceBus.pptx
Azure EventGrid vs Azure ServiceBus.pptx
 
iUpgradable
iUpgradableiUpgradable
iUpgradable
 
Bootstrap 5 whats new
Bootstrap 5   whats newBootstrap 5   whats new
Bootstrap 5 whats new
 
Blazor
BlazorBlazor
Blazor
 
Angular Form Validations
Angular Form ValidationsAngular Form Validations
Angular Form Validations
 
Introduction to NuoDB
Introduction to NuoDBIntroduction to NuoDB
Introduction to NuoDB
 
Microsoft Dynamics CRM 2013 Customization
Microsoft Dynamics CRM 2013 CustomizationMicrosoft Dynamics CRM 2013 Customization
Microsoft Dynamics CRM 2013 Customization
 
Windows PowerShell
Windows PowerShellWindows PowerShell
Windows PowerShell
 
Car care
Car careCar care
Car care
 
Tale of the photo camera
Tale of the photo cameraTale of the photo camera
Tale of the photo camera
 
Visual Studio Unleashed - Tips and Tricks
Visual Studio Unleashed - Tips and TricksVisual Studio Unleashed - Tips and Tricks
Visual Studio Unleashed - Tips and Tricks
 
What’s new in Visual Studio 2010
What’s new in Visual Studio 2010What’s new in Visual Studio 2010
What’s new in Visual Studio 2010
 

Recently uploaded

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 

Recently uploaded (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 

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.)
  • 26.
  • 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