Skip to main content

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

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
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
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.
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.
Unknown said…
Hello, you say that top n rows will be displayed, what about downloaded data? it also will be filtered or unfiltered data will be rendered?? thank you
Jessica M. Moss said…
Hi Irina,

All rows are queried/downloaded from the source. Only the top N rows will be visible. If you only want to query/download N rows, try this method: http://jessicammoss.blogspot.com/2008/09/display-top-n-rows-alternate-method.html.

HTH,
Jessica
Jatin Shah said…
I have same kinda thing but in value I specified expression based on multivalued text type parameter which I am using as an input

=IIF(Parameters!Task.Count > 2,Parameters!Task.Value(2),"")

If I am spcifying "string" instead of Parameters!Task.Value(2) it works fine. I also tried by using CStr function but its not helping.
Jessica M. Moss said…
Hi Jatin,

The IIF function evaluates both the true and false parts, so I'm wondering if it's failing because there are less than 3 items? Try adding a few more elements in the task parameter and see if that helps.

HTH,
Jessica
CG said…
You first posted this years ago and it is still valuable information! Setting the filter on the tablix property was the only way that "SELECT TOP (@Parm) * ..." would work for me in the dataset. Thank you, thank you, thank you!!!
Anonymous said…
the same I am trying to do but its give me following error message
"A filter value in the filter for tablix specified data typethat is not supported by the 'TopN' operator. verify that the data type for each filter value is integer.
Unknown said…
Excellent, this approach is better than having "top(x)" directly in data source (sql statement). i.e. I have a data source common for few tablix-es, each tablix with it's own order. Setting the "top x" in tablix offers the necessary flexibility.
Marie Chase said…
Hi ggreat reading your post

Popular posts from this blog

SSIS Configuration to Configuration to Configuration Schema

I've gotten several requests to put down in writing the configuration schema that I use as the base of my SQL Server Integration Services framework. It contains a set of configurations: an indirect environment variable configuration, which points to an XML configuration file configuration, which points to a SQL Server configuration. I learned this configuration from the Project REAL Reference Implementation . If you're getting started with a BI implementation, I highly recommend that you download it for some great ideas and best practices. Steps to implement: 1) Create an environment variable on your machine with the name of SSIS_CONFIG_FILE and the value of: C:\SSIS\Config\MasterConfigFile.dtsConfig. 2) Create an SSIS configuration file at C:\ SSIS\Config\MasterConfigFile.dtsConfig with the line: <configuration valuetype="String" path="\Package.Connections[CONFIG_SERVER].Properties[ConnectionString]" configuredtype="Property"><configuredv

Execute SQL Task Designer Limit

After migrating a package from DTS to SSIS, I had a problem with an Execute SQL Task. I couldn't change any characters in the SQLStatement property; I couldn't add any new characters; I could delete characters, but not retype them! After googling several variations of "integration services" "read only" and "Execute SQL Task", I deleted about half of the entry in a fit of frustration. Lo and behold, I could type again. Apparently, there is limit on the size or number of characters that can be entered in the SQLStatement property. From my experimentation, I came up with a limit of 32767 characters. The interesting thing is that the restriction only seems to be on the designer. If you set the SourceType to "Variable" and use a variable that contains more than 32767 characters, the task will execute. Also, if you use the "Direct Input" SourceType and modify the package XML to set the SQLStatement longer than 32767 characters,

Reporting Services 2008 Configuration Mistake

To start working with the management side of SQL Server Reporting Services 2008, I decided to set up a report server and report manager. Unfortunately, I made a mistake while setting up my configuration that left me a little perplexed. Here are the steps I took to cause, track down, and solve the issue. Problem: I began by opening the Reporting Services Configuration Manager from the Start Menu. I clicked through each of the menu options and accepted the defaults for any question with a warning symbol, since warning symbol typically designate an action item. After two minutes, all of the warning symbols had disappeared, and I was ready to begin managing my report server. Unfortunately, opening up a browser and trying to open up the report manager resulted in the dreaded " The report server has encountered a configuration error. (rsServerConfigurationError) " message. Sherlock-ing it: I put on my sleuthing hat and went to the log file directory: C:\Program Files\Microsoft