How to improve a case statement that uses two columns

后端 未结 2 1144
南笙
南笙 2021-02-12 01:54

I have a table called Purchase which has a State column, where 1 is authorized, 2 is completed (there are some other values too).

I also have a Retailer table, which has

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-12 02:15

    You could do it this way:

    -- Notice how STATE got moved inside the condition:
    CASE WHEN STATE = 2 AND RetailerProcessType IN (1, 2) THEN '"AUTHORISED"'
         WHEN STATE = 1 AND RetailerProcessType = 2 THEN '"PENDING"'
         ELSE '"DECLINED"'
    END
    

    The reason you can do an AND here is that you are not checking the CASE of STATE, but instead you are CASING Conditions.

    The key part here is that the STATE condition is a part of the WHEN.

提交回复
热议问题