WHERE子句中的CASE

时间:2013-09-16 15:07:57

标签: sql oracle

WHERE
--from TABLE.PARAMETER1
    (b.is_ot_paycode = 'Y' OR 
-- from variableA
    b.is_ot_paycode = 'Y')
    AND
-- from TABLE.PARAMETER1
    (b.is_wh_paycode = 'Y'  OR 
-- from variableA
    b.is_wh_paycode = 'Y') 
-- from variableB
    AND b.is_ot_paycode = 'Y' 

如何在我的CASE子句中创建WHERE语句

b.is_wh_paycode = 'Y' (from variableA) and 
b.is_ot_paycode = 'Y'(from variableB) 
b.is_wh_paycode (from TABLE.PARAMETER1) would always be equal to 'N'?   

1 个答案:

答案 0 :(得分:1)

您不需要case

WHERE (b.is_wh_paycode <> 'Y' or b.is_ot_paycode <> 'Y' or b.is_wh_paycode = 'N')

您可以将其编写为case

WHERE b.is_wh_paycode = (case when b.is_wh_paycode = 'Y' and b.is_ot_paycode = 'Y' 
                              then 'N' else b.is_wh_paycode
                         end)

但它不一定更清楚。

注意:这两个都假设列不会采用NULL值。如果他们这样做,那么SQL需要考虑到这一点。