简体   繁体   English

在 SSRS 报告中使用 count(*) 查询

[英]Using a count(*) query in SSRS Report

I want to use a query in SSRS report dataset as我想在 SSRS 报告数据集中使用查询作为

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

However, SSRS allows filters only on the fields that are retrieved by the query.但是,SSRS 只允许对查询检索到的字段进行过滤。

Can anyone tell me how to apply the parameter filter to the above query and use in SSRS ?谁能告诉我如何将参数过滤器应用于上述查询并在 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.您可以使用 AND 和 OR 在 IIF 中添加其他逻辑。

The IIF will do your logic check. IIF 会做你的逻辑检查。 If your criteria is met, a 1 is returned else a 0.如果满足您的条件,则返回 1,否则返回 0。

The SUM then sums up all the values from the IIF for all the records.然后,SUM 将 IIF 中所有记录的所有值相加。

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.只要在数据集查询中完成计数,您就可以计算任何您喜欢的内容,SSRS 不关心您的查询是什么,只要它有效,它只关心返回的内容。

So let's say you have 2 parameters @Date and @Category then your dataset query could simply be因此,假设您有 2 个参数@Date@Category那么您的数据集查询可能只是

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. SSRS 只会看到数据集中的myCount字段,其中包含您所需的数字。

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.我会在原始查询中选择您的日期字段,以便它可以用于参数,然后对数据集中的计数列求和而不在 datefieldname 上分组以聚合过滤结果。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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