2014-11-05 114 views
0

我想在我的SQL代碼中包括一個條件where語句,我不確定如何做到這一點...目前它看起來像這樣(見下文),雖然我是相當肯定我在這裏走錯了方向。包括一個條件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 
+1

您是否嘗試過執行它?此外,這是一個非常受歡迎的問題... – Andrew 2014-11-05 17:23:13

回答

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')) 
) 
+0

非常感謝!這工作完美,我沒有意識到你可以使用括號來配對過濾器語句。 – Moehling 2014-11-05 17:49:06

3

取而代之的case使用

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