简体   繁体   中英

Using a count(*) query in SSRS Report

I want to use a query in SSRS report dataset as

Select count(*) from tablename where datefieldname<=ParameterDate

However, SSRS allows filters only on the fields that are retrieved by the query.

Can anyone tell me how to apply the parameter filter to the above query and use in SSRS ?

Thanks

I think you want something like:

=SUM(IIF(Fields!SOME_DATE.Value <= Paramaters.ParameterDate.Value, 1, 0))

You can add other logic in the IIF with AND and OR.

The IIF will do your logic check. If your criteria is met, a 1 is returned else a 0.

The SUM then sums up all the values from the IIF for all the records.

In your dataset query use the following.

select count(JoiningDate) 
from tableName
where JoiningDate <= @parameterDate 
and department = @someDepartment 
and city like ('%' + @someCity + '%') 

and whatever other conditions you need. You will need to have other parameters unless you will always want the same department and city. These parameters can be changed on runtime to get different results.

You can count whatever you like as long as the count is done in the dataset query, SSRS does not care what your query is as long as it works, all it cares about is what is returned.

So let's say you have 2 parameters @Date and @Category then your dataset query could simply be

SELECT COUNT(*) AS myCount FROM myTable WHERE myDateColumn <= @Date AND myCategoryColumn = @Category

SSRS will just see the myCount field in the dataset with your required number in.

That's all there is to it.

I would select the your date field in the original query so it can be used against the parameter, then sum the count column in your dataset without grouping on datefieldname to aggregate your filtered results.

select datefieldname, count(*) as date_total
from tablename 
where datefieldname<=ParameterDate
group by datefieldname

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM