RaphX
RaphX

Reputation: 103

Can mutiple OR conditions be put inside a CASE function?

I think this means consider the first case or second case or so on.If any one of them is true, return 1. Am I right?

case when upper(casetype) = 'ADHOC_SUCCESSFUL' 
       then 1 
     when casetype like 'Boundary_Issue_T%' 
       then 1 
     when upper(casetype) like '%ACTIVE_SUC%' 
       then 1 
End as Successful

Upvotes: 0

Views: 58

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269743

A case expression guarantees that the conditions are evaluated in order. The expression returns the then clause associated with the first when that evaluates to true.

In this case, you could write this as:

(case when upper(casetype) = 'ADHOC_SUCCESSFUL' or
           casetype like 'Boundary_Issue_T%' or
           upper(casetype) like '%ACTIVE_SUC%' 
      then 1 
 end) as Successful

I would also be inclined to add else 0, so the result is either 0 or 1, rather than NULL or 1.

Redshift supports boolean types, so you can also do:

(upper(casetype) = 'ADHOC_SUCCESSFUL' or
 casetype like 'Boundary_Issue_T%' or
 upper(casetype) like '%ACTIVE_SUC%' 
) as Successful

Upvotes: 1

Related Questions