Please see my other Database Development articles.
There are several new impressive advancements in the latest version of t-sql; this article will focus on “Ranking” functions.
Prior to the release of SQL Server 2005 with its t-sql enhancements, working with blocks of related data was clunky at best. T-sql authors mostly relied on “GROUP BY” statements and then had to perform “CURSOR” and/or “SORT BY” acrobatics in order to return contiguous blocks of data in any meaningful manner.
Ranking functions overcome previous limitations to working with subsets of data within implicit groups.
In other words, SQL Server developers are now able to in essence, perform “GROUP BY” operations within existing groups.
We can achieve such actions by choosing from several segregation actions according to the desired result along with the familiar “SORT BY” finishing touches.
To begin these examples, first I’ll show you the source data.
It’s simply a series of records reflecting average data about people such as would be common to most databases .
For example, for all of our example will be working with the following fields: FirstName, Age, and Gender.
Our first example will illustrate the most basic ranking function, “ROW_NUMBER().”
The primary purpose of this function is simply to add sequential numbering unto an existing t-sql feature, “ORDER BY.” In other words, just think of how you were already using the “ORDER BY” clause, but with an additional column of ordered numbers and you’ll understand the “ROW_NUMBER()” function.
In this example, I wanted to order everyone according to their “Age” and then see that list order by a sequential “Row Number by Age” column.
ROW_NUMBER(), part 2
In this example, you’ll see the same function being called by I wanted the data returned in its natural state – the row order left unchanged.
In this manner, I can have the same sequential numbered column according to “Age,” but without the rowset modified.
ROW_NUMBER() with PARTITION
Here’s where the magic of ranking functions really come into focus – with PARTITIONing!
Have you ever wanted to perform a “GROUP BY” within itself? Now you can!
Use the “ROW_NUMBER()” function just as in the other example, except this time specify by which field you’d like the inner grouping to occur.
In this example, I chose to separate the age groups according to gender. Then, I perform a “ORDER BY” on each subset.