简体   繁体   English

在使用案例的Where子句中使用参数化列

[英]Using a Parameterized column in a Where Clause using Case

I am building a report in SSRS where I need to be able to let our users filter on different date fields. 我正在SSRS中构建报告,在这里我需要能够让我们的用户在不同的日期字段上进行过滤。 They need to be able to select the date filter and then the start and end dates within that filter. 他们需要能够选择日期过滤器,然后选择该过滤器中的开始日期和结束日期。

This is a simplified Example of what I am trying to achieve: 这是我要实现的简化示例:

SELECT Project.ProjectID, Project.Name, Project.Update_Date, Project.Due_Date, Project.Start_Date, Project.Review_Date
FROM Project
WHERE @datetypefiler BETWEEN @startdate AND @enddate

Where the @datetypefiler will essentially equal the field the user wishes to filter on (ie Project.Update_Date or Project.Due_Date etc) @datetypefiler基本上等于用户希望过滤的字段(即Project.Update_Date或Project.Due_Date等)

On SSRS i built parameters for @datetypefiler which could equal the various date fields and I set up the start date and end date filter as expected. 在SSRS上,我为@datetypefiler构建了可以等于各个日期字段的参数,并且按预期设置了开始日期和结束日期过滤器。

Unfortunately it does not seem as though I can parameterize the @datetypefiler in this way? 不幸的是,似乎我无法以这种方式参数化@datetypefiler? I then attempted to use the Case keyword as so: 然后,我尝试这样使用Case关键字:

SELECT Project.ProjectID, Project.Name, Project.Update_Date, Project.Due_Date, Project.Start_Date, Project.Review_Date
FROM Project
WHERE CASE 
when @datetypefiler='Project.Update_Date' then Project.Update_Date BETWEEN @startdate AND @enddate
when @datetypefiler='Project.Due_Date' then Project.Due_Date BETWEEN @startdate AND @enddate

This isnt working for me either, I figure that instead of taking shots in the dark I should ask what the correct method would look like. 这对我也不起作用,我认为与其在黑暗中拍照,不如问正确的方法是什么样。 Any advice on this would be greatly appreciated, 任何对此的建议将不胜感激,

You could try something like this: 您可以尝试这样的事情:

SELECT * 
FROM Project
WHERE  (@datetypefiler='Project.Update_Date' 
                    AND Project.Update_Date BETWEEN @startdate AND @enddate)
    OR (@datetypefiler='Project.Due_Date' 
                    AND Project.Due_Date BETWEEN @startdate AND @enddate)

The OR will only evaluate the full statement if the @datetypefiler is matched. 如果@datetypefiler匹配,则OR只会评估完整语句。

Or you could try this 或者你可以尝试这个

SELECT Project.ProjectID, Project.Name, Project.Update_Date, Project.Due_Date, 
Project.Start_Date, Project.Review_Date 
FROM Project
where 
(CASE 
when @datetypefiler = 'Project.Update_Date' then Project.Update_Date 
when @datetypefiler= 'Project.Due_Date' then Project.Due_Date 
END) BETWEEN @startdate AND @enddate

While both of other two answers are going to work just fine, they won't make use of index. 虽然其他两个答案都可以正常工作,但它们不会使用索引。 I'd recommend to have two separate SELECT statements with two different queries based on your criteria, this way you can index your table both on Update_Date and Due_Date and actually use them to speed your queries up. 我建议根据您的条件使用两个不同的查询使用两个单独的SELECT语句,这样您就可以在Update_DateDue_Date上对表进行Update_Date ,并实际使用它们来加快查询速度。

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

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