Thursday, August 14, 2008

Display Top N Rows

In SQL Server Reporting Services, you can dynamically limit the number of rows that are displayed in a table or a matrix. Here are the steps to create a sample report showing this.

1. Create a DataSource connecting to the AdventureWorksDW database and a DataSet with the following query:
SELECT DimEmployee.FirstName, DimEmployee.LastName, SUM(FactResellerSales.SalesAmount) AS SalesAmount
FROM DimEmployee INNER JOIN
FactResellerSales ON DimEmployee.EmployeeKey = FactResellerSales.EmployeeKey
WHERE (FactResellerSales.OrderDateKey = 20011001)
GROUP BY DimEmployee.FirstName, DimEmployee.LastName

2. Add a report parameter named “NumberPpl” of data type integer.

3. Add a table to the body of the report with the data fields of firstName, lastName, and salesAmount. Sort by the column salesAmount from ‘Z to A’.

4. Set the Filters option of the table as shown below. (This is where the real work is happening!)

5. Preview the report, modifying the value for the parameter NumberPpl to see the number of rows change. It should look similar to the reports below.



Version: SQL Server 2008 RC0

18 comments:

Anonymous said...
This comment has been removed by a blog administrator.
yOOrek said...

Thanks, this has helped me a lot. I was trying to achieve this in SQL statement with a parameter but it didn't work. The way you described it is perfect.
Thanks,
yOOrek

Hugo Eduardo said...

Hi Jessica,
This same case should I apply to RS 2005 but I get an error that says:

"An error occurred during local report processing.
An error has occurred during report processing.
Failed to evaulate the FilterValue."

Thanks for the help.

Jessica M. Moss said...

Hi Hugo,

The same logic should work for 2005 as well. Double check the parameter name in the Value field, and ensure that you are using brackets, an at symbol, and the exact name of the Parameter.

HTH, Jessica

David Carrington said...

This mostly works, except in my particular case I'm sorting on a column which has a few identical values, so when I say to give me the "Top 5", it gives me 8, since the last 4 all have the same value.

David Carrington said...

Further - my solution was to use Top N on another unique column at the same time.

Jessica M. Moss said...

Hi David,

Try using the alternate method: http://jessicammoss.blogspot.com/2008/09/display-top-n-rows-alternate-method.html. Make sure you order by an extra column, so that you can guarantee which of the four rows will be shown.

Good luck!
Jessica

Anonymous said...

Jessica,

Thanks! Google pointed me right to this. It is exactly what I was looking for!

Steve

Anonymous said...

Hugo, that error occurs if you create the parameter with the wrong data type - such as text. Change it to an integer and it should work

Gilbert P said...

Awesome! Thanks for this.

Anonymous said...

Awesome. This worked perfectly.

Thanks,
Michael

Anonymous said...

Hi Jessica,

when Im trying this, I get an error
Microsoft Sql Server Repport Designer

value is not a decimal.

even though in parameter i have changed the data type to interger. I have also tried to change it to float as well.

JimC said...

Hi, Jessica

Thanks for the blog. I was wondering if there is a way to report the SUM of Top N's Amount?

Thanks,
Jim

Jessica M. Moss said...

Hi Jim,

You may want to use the alternative top n method found here: http://jessicammoss.blogspot.com/2008/09/display-top-n-rows-alternate-method.html. That will allow you to sum.

Jessica

Andrea Grimaldi said...

I'm completely new at this. Would you please tell me how to create a report parameter? Please....

Jessica M. Moss said...

Andrea,

Please research "Creating report parameters in SSRS" in Google or Bing for information on creating report parameters.

Jessica

Anonymous said...

Thank you so much!
Everything perfect is simple!
You are the best.

Anonymous said...

Very Nice.