WHERE IF(字段取决于参数)SQL

Dmi*_*try 0 sql sql-server reporting-services sql-server-2014

初始数据示例:

 Project   |   Field1   |   Field2    |   Field3    |
 --------------------------------------------------------------------
 Project 1 |      0     |      1      |      2      |
 Project 2 |      2     |      0      |      1      |
Run Code Online (Sandbox Code Playgroud)

使用SQL Server 2014,我有表,我需要在ssrs参数中获取依赖于值的数据.查询样本.

Select *
Where IF@Scenario=1, then Field1 < 2
      IF@Scenario=2, then Field2 < 2
      IF@Scenario=3, then Field3 < 2
From Table1
Run Code Online (Sandbox Code Playgroud)

Ben*_*eno 5

Select *
FROM x
Where 
      2 > 
      CASE @Scenario WHEN 1 THEN Field1 
           WHEN 2 THEN Field2
           WHEN 3 THEN Field3
           ELSE 0 --This part is just to avoid errors when scenario isnt in any of the options. 2>0 will display everything
      END
Run Code Online (Sandbox Code Playgroud)

将条件(2)与列进行比较.