建议SQL Server中的WHERE条件

时间:2014-04-21 15:36:33

标签: sql-server sql-server-2008 tsql reporting-services ssrs-2008-r2

这里是已编辑的代码:此代码更接近我正在运行的实际代码。它给了我除取消记录以外的所有记录。我想在Prod列中包含带有NOT NULL字段的已取消记录。

Select  AppID,
    Prod1, 
Prod2, 
Prod3, 
Prod4, 
Prod5, 
Prod6, 
Prod7, 
Prod8,
CASE WHEN len(Prod1) > 0 OR
   len(Prod2) > 0 OR
       len(Prod3) > 0 OR
       len(Prod4) > 0 OR
       len(Prod5) > 0 OR
       len(Prod6) > 0 OR
       len(Prod7) > 0 OR
       len(Prod8) > 0 THEN 1 ELSE 0 END AS NbrIncomplete
WHERE      (CASE WHEN LEFT(ProductShortName, 2) IN ('CT', 'GR') THEN 1 ELSE 0 END = 0) AND  
       (CTRACKS.Ctrack IS NULL) AND (AppInitiatedDate >= @StartDate) AND (AppInitiatedDate < @EndDate) AND 
       (Status <> N'Cancelled' OR
    Status IS NULL) 
OR
       (CASE WHEN LEFT(ProductShortName, 2) IN ('CT', 'GR') THEN 1 ELSE 0 END = 0) AND 
       (CTRACKS.Ctrack IS NULL) AND (Status <> N'Cancelled' OR
        Status IS NULL) AND 
   (CASE WHEN 
         len(Prod1) > 0 OR
             len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 
                     THEN 1 ELSE 0 END = 1)
ORDER BY AppID

5 个答案:

答案 0 :(得分:3)

根据所需输出的样本,您需要:

SELECT AppID, Status, Prod1,Prod2,Prod3,Prod4,Prod5,Prod6,Prod7,Prod8
FROM   Status
WHERE  Coalesce(Prod1, Prod2, Prod3, Prod4, Prod5, Prod6, Prod7, Prod8) IS NOT NULL
   OR  Status <> 'Cancelled' 

这假设一个合理的DB模式,这样Prod1等列的值为NULL,而不是空字符串。如果它们是空字符串,则应修复架构


刚刚看到您对Prod [n]类型的评论确实是nvarchar,我将重新进行迭代:您应该修复数据库架构。你拥有的是破碎。除了使这个查询比它需要的更复杂之外,几乎不可能准确地比较日期以找到诸如特定行的prod [n]之类的东西(我也建议拉那些)产品字段分为单独的表,该表的密钥加上产品编号和日期作为列)。

但是在短期内,还不知道报告中的空值是NULL还是空字符串,你可以这样做:

SELECT    AppID, Status, Prod1,Prod2,Prod3,Prod4,Prod5,Prod6,Prod7,Prod8
FROM      Status
WHERE     Status <> 'Cancelled' OR 
           LEN(coalesce(Prod1,''))>0 or
           LEN(coalesce(Prod2,''))>0 or
           LEN(coalesce(Prod3,''))>0 or
           LEN(coalesce(Prod4,''))>0 or
           LEN(coalesce(Prod5,''))>0 or
           LEN(coalesce(Prod6,''))>0 or
           LEN(coalesce(Prod7,''))>0 or
           LEN(coalesce(Prod8,''))>0  
ORDER BY   AppID

我提供的是因为您可能有人需要此数据现在,但是再次:您的下一个任务应该是修复架构以使用{{1} } types。

答案 1 :(得分:1)

where where子句不会像你想要的那样工作。你需要这样的东西:

where
(status = 'CANCELLED' and (PROD1 is not null or prod2 is not null<etc>))
or
status <> 'CANCELLED'

这是一个简单的例子: SQL Fiddle

答案 2 :(得分:0)

IS NOT NULL而不是>0
所以:WHERE Status <> 'Cancelled' AND (Prod1 IS NOT NULL or Prod2 IS NOT NULL or Prod3 IS NOT NULL or Prod4 IS NOT NULL or Prod5 IS NOT NULL or Prod6 IS NOT NULL or Prod7 IS NOT NULL or Prod8 IS NOT NULL)

答案 3 :(得分:0)

Select * from status 
    where (Status<>'Cancelled' and Prod1 is not null and...and Prod8 is not null) or Status='Cancelled'

答案 4 :(得分:0)

最后,我能够成功运行代码并获得所需的输出。 Yeyy !!!

Select  AppID, 
Prod1, 
Prod2, 
Prod3, 
Prod4, 
Prod5, 
Prod6, 
Prod7, 
Prod8,
CASE WHEN len(Prod1) > 0 OR
   len(Prod2) > 0 OR
   len(Prod3) > 0 OR
   len(Prod4) > 0 OR
   len(Prod5) > 0 OR
   len(Prod6) > 0 OR
   len(Prod7) > 0 OR
   len(Prod8) > 0 THEN 1 ELSE 0 END AS NbrIncomplete
WHERE      
   (CASE WHEN LEFT(ProductShortName, 2) IN ('CT', 'GR') THEN 1 ELSE 0 END = 0) AND  
   (CTRACKS.Ctrack IS NULL) AND ((Status = 'Cancelled' AND 
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL)) OR Status <> 'Cancelled')
   AND 
   (AppInitiatedDate >= @StartDate) AND (AppInitiatedDate < @EndDate)  
   OR
   (CASE WHEN LEFT(ProductShortName, 2) IN ('CT', 'GR') THEN 1 ELSE 0 END = 0) AND 
   (CTRACKS.Ctrack IS NULL) AND 
   (CASE WHEN 
     len(Prod1) > 0 OR
         len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 
                 THEN 1 ELSE 0 END = 1)
  ORDER BY AppID

感谢您的努力。欣赏它。