in

jetlounge

the minty fresh blog that whitens your teeth!


Syndication


Get Ranking of Groups in SSRS - Top X Ranking

by David Leibowitz | December 16, 2007 | Comments: 7

Often you'd like to view a row number, or rank on a report in SQL Server Reporting Services. This is simple enough on a line item detail report with the RowNumber function. The reference to Nothing sets our scope.
Just use:
=RowNumber(Nothing)


But now how about something a little more challenging. Perhaps we have a list of 1000 Customers and we group them by State on a table. Perhaps the results will be sorted in order of Sales in decending order by State. Perhaps this is a Top 10 Report? Generally on these types of reports, it's nice to illustrate a ranking next to each row, not just the resulting data. To do so we'll need a different combination of functions to get the desired result.
First, create your detail, and a simple grouping by State. In the State Row, add a column. To this we will add the expression:

=RunningValue(THEFIELDYOUGROUPON, CountDistinct, Nothing)

or, in this example

=RunningValue(Fields!State.Value, CountDistinct, Nothing)

What might have you shaking your head is the use of CountDistinct. That's essentially what a grouping will do, in T-SQL, in SSRS...it's going to distinct your values and do some magic. Samples on the web usually illustrate the use of the Count function, but that will not produce the desired result in any grouping row.

 

del.ico.us digg this technorati BlinkList Furl reddit DotNetKicks google! live Facebook Stumble Upon Yahoo!



Comments

 

Naidu said:

Hi,  Thank you for the post on ranking article. Really this helped me a lot. Thanks again. actually it is saved me a lot rewrite code at db site.

July 27, 2009 12:05 PM
 

thomas said:

oh dear god THANK YOU i have been at this innocuous little diddy for HOURS

October 27, 2010 8:32 PM
 

Sabitha said:

Thanks for the code.It helped me a lot.

January 5, 2011 1:59 AM
 

Ambika said:

Thanks, really helped me a lot. Now how do you restrict it to show the Top X

June 2, 2011 5:26 PM
 

seena said:

Thanks it really helped me a lot ,its save my time a lot

July 12, 2011 12:29 AM
 

Russell Fox said:

THANK YOU!  I've been banging my head against this for about three hours.

July 14, 2011 6:14 PM
 

Dhinesh said:

I need to do the function in SSRS like

D5= IF(C5=0,D5=1,D4+1) the formula which we are currently in Excel

July 22, 2011 6:07 AM

Leave a Comment

(required)  
(optional)
(required)  
Add
Powered by Community Server (Non-Commercial Edition), by Telligent Systems