建议更好的WHERE条件

时间:2014-04-19 14:42:06

标签: sql sql-server

我有以下查询:

SELECT AppInitiatedDate,
       Incomplete.Product1,
       Incomplete.Product2, 
       Incomplete.Product3, 
       Incomplete.Product4, 
       Incomplete.Product5, 
       CASE WHEN 
         len(Incomplete.Product1) > 0 or
         len(Incomplete.Product2) > 0 or
         len(Incomplete.Product3) > 0 or
         len(Incomplete.Product4) > 0 or
         len(Incomplete.Product5) > 0 THEN 1 ELSE 0 
       END AS NumberIncomplete,
       CASE WHEN AppInitiatedDate >= @StartDate AND AppInitiatedDate < @EndDate 
            THEN 1 
            ELSE 0 
       END AS NumberInitiated
FROM [TableNames]
WHERE AppInitiatedDate >= @StartDate) 
  AND AppInitiatedDate < @EndDate) 
  AND  **(DecisionStatus.Status in ('Active', 'Inactive', 'Complete'
         , 'Cancelled', 'WithDrawn', 'CounterOffer') 
   OR (DecisionStatus.Status = 'Cancelled' 
       AND DecisionStatus.Status IS NOT NULL)** 
--DecisionStatus table inclues fields like 
--Active, Inactive, Complete, Cancelled, WithDrawn, CounterOffer etc. ALso 

方案是:我希望不完整产品应用程序的所有记录都包含在DecisionStatus表中的所有状态,但不希望取消所有记录为空的已取消记录。请建议我是否可以提出上述问题。

1 个答案:

答案 0 :(得分:1)

我不知道我是否正确理解你的观点,但试试这个

WHERE AppInitiatedDate >= @StartDate)    AND AppInitiatedDate < @EndDate)    AND IsNull(DecisionStatus.Status,'Cancelled') <> 'Cancelled'