What is a Windowing Function?

I have been presenting and writing about windowing functions for a couple of years. I am always surprised that many people who don’t know what these are may already be writing queries using them. Every time I present on this topic at a SQL Saturday, I get at least one evaluation comment saying that they thought the session would be about the Windows operating system, not T-SQL.

These functions, called window, windowed or windowing, are part of the ANSI SQL standards, not something proprietary to SQL Server or to Microsoft. One thing that windowing functions require is an OVER clause that defines the window or set of rows that the function will operate on. You’ll also find windowing functions in only two places in a query: the SELECT and OVER clauses.

There were two types of windowing functions introduced with SQL Server 2005: the ranking functions, ROW_NUMBER(), RANK, DENSE_RANK() and NTILE() and window aggregates. Almost everyone is using or at least familiar with ROW_NUMBER(), but not many people have heard about window aggregates.

Window aggregates allow you to add an aggregate expression to a query without making it an aggregate query. This makes writing some queries very simple. The unfortunate thing, however, is that this technique doesn’t perform as well as some older methods. Here is an example:

 

SELECT CustomerID, SalesOrderID, TotalDue,
     SUM(TotalDue) OVER(PARTITION BY CustomerID) CustTotal
FROM Sales.SalesOrderHeader;

WITH cust AS(
    SELECT CustomerID,SUM(TotalDue) AS CustTotal
    FROM Sales.SalesOrderHeader
    GROUP BY CustomerID)
SELECT cust.CustomerID,SalesOrderID,TotalDue,CustTotal
FROM Sales.SalesOrderHeader AS SOH
JOIN cust ON SOH.CustomerID=cust.CustomerID;

 

The first query adds the windowing aggregate expression SUM(TotalDue) OVER(PARTITION BY CustomerID). This provides a total for each unique CustomerID. The second query is one of the traditional ways to do the same thing. Inside the CTE (Common Table Expression) a query returns a list of the CustomerIDs with the total for each. It is then joined to the table in the outer query to provide the same results. If you take a look at the execution plan or STATISTICS IO, you will see that the second method actually performs better.

To get a total over the entire result set, use the empty parentheses like this: SUM(TotalDue) OVER().

So, while the window aggregate method is so simple, you need to use it with caution. Stay tuned for many more posts about windowing functions. I promise that for the most part, they improve performance.

 

 

About Kathi Kellenberger

I am a SQL Server professional working for Linchpin People. I love talking about SQL Server to anyone who will listen, just ask my two year old granddaughter. I love to write and teach. I am so humbled and honored to say that I made SQL Server MVP in 2013.
This entry was posted in T-SQL Window Functions. Bookmark the permalink.

One Response to What is a Windowing Function?

  1. Pingback: Row Numbers and Running Totals in SSIS - Tim Mitchell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s