Go Back   CORTEX Forums > Local Happenings > CORTEX Blogs > BI Monkey
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read

Ranking and Numbering rows ? and subsets of rows ? in T-SQL

This is a discussion on Ranking and Numbering rows ? and subsets of rows ? in T-SQL within the BI Monkey forums, part of the CORTEX Blogs category; I recently had to deal with a scenario where I needed to pivot out some rows after ordering (ranking) them according to specific rules so I could present some rows ...


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 19th May 2011, 10:43 PM   #1
Guru
 
Join Date: Jun 2009
Posts: 135
James Beresford is on a distinguished road
Thumbs up Ranking and Numbering rows ? and subsets of rows ? in T-SQL

I recently had to deal with a scenario where I needed to pivot out some rows after ordering (ranking) them according to specific rules so I could present some rows of data as columns, but in a specific order (don’t ask why, it’ll make me grind my teeth about data analysts that don’t understand how to analyse data…). The ordering in itself was only part of the solution, as to Pivot the data, the keys need to be specified in the query, so the natural keys can’t be used. The scenario is set out below:

Fig 1: Rank and Pivot. The Rank column needed to be added


My first thought was that I’d have to solve this with a cursor, which wasn’t a practical option as there were 1.5m rows of data to process, and if my solution involves a cursor I instantly think it’s a lousy solution. However I was pleased to discover the T-SQL function ROW_NUMBER() which allows you to add row numbering to ordered data and even subgroups of that data. (The below samples use the AdventureWorks2008 database.)

First up, basic row numbering:
SELECT ROW_NUMBER() OVER (ORDER BY ProductId) AS ID_Key
,** *** *[ProductID]
,** *** *[LocationID]
,** *** *[Shelf]
,** *** *[Bin]
,** *** *[Quantity]

FROM [Production].[ProductInventory]

WHERE [ProductID] IN (1,2,3,4)

The above query adds an ID key to the data based on ordering by the ProductID field. The ROW_NUMBER() function requires an OVER clause to know on what basis it should assign the key, and this has to be an ORDER BY statement. The end result looks like this:

Fig 2: Simple row numbering


You can extend this to order within a subgroup, by specifying a PARTITION BY clause so ROW_NUMBER() operates with that subgroup. In the example below I partition by ProductId:
SELECT ROW_NUMBER() OVER (PARTITION BY ProductId ORDER BY Quantity DESC) AS Subset_ID_Key
,** *** *[ProductID]
,** *** *[LocationID]
,** *** *[Shelf]
,** *** *[Bin]
,** *** *[Quantity]

FROM [Production].[ProductInventory]

WHERE [ProductID] IN (1,2,3,4)

Which yields this result, with the ranking now only applying within a Product Id:

Fig 3: Row numbering within a Subgroup


Which can then be pivoted on the rank, as the key of the rank is now known:
SELECT ProductID
,** *** *[1] AS Bin_1
,** *** *[2] AS Bin_3
,** *** *[3] AS Bin_3

FROM

(

SELECT ROW_NUMBER() OVER (PARTITION BY ProductId ORDER BY Quantity DESC) AS Subset_ID_Key
,******* [ProductID]
,******* [Bin]

FROM [Production].[ProductInventory]

WHERE [ProductID] IN (1,2,3,4)

) AS Pivot_Source

PIVOT

(
MAX(Bin)
FOR Subset_ID_Key IN ([1],[2],[3])
) AS Pivot_Output

Which yields this final output:

Fig 4: Ranked and Pivoted


All done within a single query, and not a cursor in sight. ROW_NUMBER() was a great function to discover!

MSDN Documentation is here for:
  • ROW_NUMBER() – the key function
  • OVER – ordering and subgrouping the results of ROW_NUMBER
  • PIVOT – for pivoting out the results

Get More from the original blog...
James Beresford is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiTweet this Post!
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Google search ranking algorithm deemed unfair by European Commission admin Analytic News Feeds 0 2nd December 2010 09:25 AM
FusionIQ - Quantitative Ranking system fusing technical and fundamental variables ... Latest News Headlines 2010 Q1 News Headlines 0 16th March 2010 02:57 AM
New aggregation and ranking alogorithm with self-weighing admin Analytic News Feeds 0 17th February 2010 05:04 PM
Ranking Schools Chris Lloyd Fishing In The Bay 0 1st February 2010 03:54 PM
MySQL: Another Ranking trick Latest News Headlines DWH Tip Feeds 0 25th November 2009 11:03 AM


All times are GMT +11. The time now is 10:44 PM.

© The Business Intelligence Group

Search Engine Optimization by vBSEO