简体   繁体   English

WHERE子句中的CASE表达式选择不为NULL的行

[英]CASE expression in WHERE clause selecting rows that are not NULL

I'm trying to write a query that when passed two variables, the first set is numbers 1-4 and the second set of number is 1-2. 我正在尝试编写一个查询,该查询在传递两个变量时,第一组为数字1-4,第二组为1-2。 I want to select only the rows that meet the conditions. 我只选择符合条件的行。 I've come up with this so far and am now a little lost on how to call the rows with my results. 到目前为止,我已经提出了这个建议,现在我对如何用结果调用行有些迷茫。

DECLARE @Task INT 
DECLARE @Status INT 

SET @Task = 1 --Test Values can be 1,2,3,4
SET @Status = 1 -- Test Values can be 1,2

SELECT  
     Pics
    ,Title
    ,Photos
    ,Reports
FROM    
    DATES
WHERE   
    CASE 
       WHEN @Task = 1 AND @Status = 1 THEN Pics IS NOT NULL
       WHEN @Task = 2 AND @Status = 1 THEN Title IS NOT NULL 
       WHEN @Task = 3 AND @Status = 1 THEN Photos IS NOT NULL
       WHEN @Task = 4 AND @Status = 1 THEN Reports IS NOT NULL              
       ELSE NULL
    END

Sample data: Here is what i am using this for. 示例数据:这是我正在使用的目的。

I have 2 drop down boxes, one called Tasks that hold the 4 values Pics, report, photos, title. 我有2个下拉框,其中一个称为“任务”,其中包含4个值的图片,报告,照片,标题。

Second box holds the complete or not completed 1 or 2 第二个盒子存放已完成或未完成的1或2

I show all 4 Reports, titles, pics, photos when this query is called, but if i want to see all the reports that are done i can select the report value and then select the complete tab. 调用此查询时,我将显示所有4个报告,标题,图片,照片,但是如果我想查看所有已完成的报告,则可以选择报告值,然后选择完整选项卡。 example (Task)Report = 3 and (Status)complete = 1. 示例(任务)报告= 3和(状态)完成= 1。

giving me all the reports that are done and still displaying the other 3 values even if they are null. 给我所有已完成的报告,即使它们为空,仍显示其他三个值。

What you want is the eternally simple AND/OR: 您想要的是永远简单的AND / OR:

DECLARE @Task INT 
DECLARE @Status INT 

SET @Task = 1 
SET @Status = 1

SELECT  
     Pics
    ,Title
    ,Photos
    ,Reports

FROM    DATES

WHERE @Status = 1
AND (
        (@Task = 1 AND Pics IS NOT NULL)
    OR
        (@Task = 2 AND Title IS NOT NULL)
    OR
        (@Task = 3 AND Photos IS NOT NULL)
    OR
        (@Task = 4 AND Reports IS NOT NULL) 
    )

Used @JohnHC example and worked off of that. 使用@JohnHC示例并解决了这个问题。 I went with a CASE after all, because it did what i needed to do. 毕竟,我使用了CASE,因为它可以完成我需要做的事情。 here is the finished code. 这是完成的代码。 Thanks for all the info and help! 感谢您提供的所有信息和帮助! If you think there is another way please let me know. 如果您认为还有另一种方法,请告诉我。

DECLARE @Task INT 
DECLARE @Status INT 

SET @Task = 1 --Test Values can be 1,2,3,4
SET @Status = 1 -- Test Values can be 1,2

SELECT  
      Pics
     ,Title
     ,Photos
     ,Reports
FROM    
      DATES

WHERE   1 = CASE WHEN @Task = 1 AND @Status = 1 AND Pics IS NOT NULL THEN 1 ELSE 0 END
      OR
        1 = CASE WHEN @Task = 1 AND @Status = 2 AND Pics IS NULL THEN 1 ELSE 0 END
      OR
        2 = CASE WHEN @Task = 2 AND @Status = 1 AND Reports IS NOT NULL THEN 2 ELSE 0 END
      OR
        2 = CASE WHEN @Task = 2 AND @Status = 2 AND Reports IS NULL THEN 2 ELSE 0 END
      OR
        3 = CASE WHEN @Task = 3 AND @Status = 1 AND Photos IS NOT NULL THEN 3 ELSE 0 END
      OR
        3 = CASE WHEN @Task = 3 AND @Status = 2 AND Photos IS NULL THEN 3 ELSE 0 END
      OR
        4 = CASE WHEN @Task = 4 AND @Status = 1 AND Title IS NOT NULL THEN 4 ELSE 0 END
      OR
        4 = CASE WHEN @Task = 4 AND @Status = 2 AND Title IS NULL THEN 4 ELSE 0 END
END

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

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