2014-03-25 126 views
0

我需要一个基于表字段值的条件Where子句或Case语句。SQL需要两个WHERE子句条件

这是我已经和它不工作:

Select n.id, n.message, n.Type 
From Notifications n 
Where 
(
     n.Type = 1 
     And ApplicationMask & isnull(@applicationMask,2147483647)<> 0 
     And Cast(GETDATE() as Date) Between Cast(StartDate AS Date) and 
              Cast(EndDate as Date) 
     And Not Exists(Select notificationID 
        From NotificationDelivery nd 
        Where nd.NotificationID = n.id 
        and nd.UserID = @userID) 

) 
OR 
(
     n.Type = 2 
     And ApplicationMask & ISNULL(@applicationMask,2147483647) <> 0 
     And Cast(GETDATE() as Date) Between Cast(StartDate AS Date) and 
              Cast(EndDate as Date)  
) 

OR是搞乱起来,我想。最上面的Where子句本身起作用,但是像这样它返回的行不应该返回。好像NOT EXIST部分被忽略。有没有更好的办法?我在SQL查询中不太强大。

+0

您是不是要加'不EXISTS'部分查询的后半部分? – Uxonith

+0

您是否希望将'NOT EXISTS'应用于顶部和底部变体? – jpw

+1

“不工作”和“返回它不应该返回的行”并不是非常具体。你能更具体吗? –

回答

1

试试这个更好的性能和更好的可读性。我认为这将返回你所需要的结果:

set @applicationMask = isnull(@applicationMask,1) 

Select n.id, n.message, n.Type 
From Notifications n 
Where 
Type in (1,2) 
And ApplicationMask = 1 
And @applicationMask > 0 
And dateadd(d, 0, datediff(d, -1, GETDATE()) > StartDate 
And dateadd(d, 0, datediff(d, 0, GETDATE())) <= EndDate 
And (type = 2 or Not Exists(Select null 
       From NotificationDelivery nd 
       Where nd.NotificationID = n.id 
       and nd.UserID = @userID)) 
+0

谢谢,你明白了,虽然datediff部分没有给我正确的结果,所以我只是把BETWEEN放回去,像我需要的那样工作。 – user3461552

+0

@ user3461552你确定这个零件没有工作吗?那部分是为了调整你的剧本。它看起来对我有效。进行小修改以匹配其他数据类型,而不是日期时间 –

0

完全未经测试,但如何组织的这样的SQL如何:

Select n.id, n.message, n.Type 
    From Notifications n 
Where 1=1 
    And (ApplicationMask & ISNULL(@applicationMask,2147483647) <> 0 
    And Cast(GETDATE() as Date) Between Cast(StartDate AS Date) 
    And Cast(EndDate as Date))  
    OR (ApplicationMask & isnull(@applicationMask,2147483647)<> 0 
    And Cast(GETDATE() as Date) Between Cast(StartDate AS Date) 
    And Cast(EndDate as Date) 
    And Not Exists(Select notificationID 
        From NotificationDelivery nd 
        Where nd.NotificationID = n.id 
        and nd.UserID = @userID)) 

只是一个想法...祝您好运。

0

我建议只是区分你的两个条件为两个查询和UNION结果

SELECT n.id, n.message, n.Type 
FROM Notifications n 
WHERE n.Type = 1 
    AND ApplicationMask & isnull(@applicationMask,2147483647)<> 0 
    AND Cast(GETDATE() as Date) Between Cast(StartDate AS Date) and 
            Cast(EndDate as Date) 
    AND Not Exists(Select notificationID 
      From NotificationDelivery nd 
      Where nd.NotificationID = n.id 
      and nd.UserID = @userID) 

UNION 

SELECT n.id, n.message, n.Type 
FROM Notifications n 
WHERE n.Type = 2 
    AND ApplicationMask & ISNULL(@applicationMask,2147483647) <> 0 
    AND Cast(GETDATE() as Date) Between Cast(StartDate AS Date) and 
             Cast(EndDate as Date)  
+0

我不会在同一张桌子上看过两遍...... –

0

我会尽力缩小对类似数据集和过滤器 -

Select 
n.id, n.message, n.Type 
From (select n.id, n.message, n.Type 
    from Notifications 
    WHERE 
    n.Type in (1, 2) 
    And ApplicationMask & ISNULL(@applicationMask,2147483647) <> 0 
     And Cast(GETDATE() as Date) Between Cast(StartDate AS Date) and 
              Cast(EndDate as Date) 
    ) n 

Where not (n.Type =1 
     And Not Exists(Select notificationID 
        From NotificationDelivery nd 
        Where nd.NotificationID = n.id 
        and nd.UserID = @userID) 
     )