包括条件sql where语句

时间:2014-11-05 17:20:38

标签: sql sql-server-2012 where where-in

我试图在我的SQL代码中包含一个条件where语句,而且我不确定该怎么做...目前它看起来像这样(见下文)虽然我是相当肯定我在这里走错了方向。

     WHERE 
     [awb_prefix] in ('403','369')
     AND [agent_iata_code] NOT IN ('0508634')
     AND [yr] = 2014
     AND CASE WHEN [awb_prefix] = '369' THEN [origin_gw_carrier] in ('PO','9S','K4') ELSE [origin_gw_carrier] in ('PO','9S','K4','5Y')
     END

2 个答案:

答案 0 :(得分:4)

这不是CASE的用途。您可以尝试这样做:

WHERE   [awb_prefix] in ('403','369')
AND     [agent_iata_code] NOT IN ('0508634')
AND     [yr] = 2014
AND 
(
    ([awb_prefix] = '369' AND [origin_gw_carrier] in ('PO','9S','K4'))
OR  ([awb_prefix] <> '369' AND [origin_gw_carrier] in ('PO','9S','K4','5Y'))
)

答案 1 :(得分:3)

而不是case使用

AND 
(
    [origin_gw_carrier] in ('PO','9S','K4') AND [awb_prefix] = '369' OR
    [origin_gw_carrier] in ('PO','9S','K4','5Y')
)