简体   繁体   中英

How to write multiple conditions(Combination of and / or) in CASE statement?

I am trying to select values based on the following case statment

CASE
when ty.type ='Catalog'
and zs.name='Aries'
or zs.name='Leo'
AND ( CASE 
when actual_finish_date is not null
then actual_finish_date
when updated_finish_date is not null
then updated_finish_date
else baseline_finish_date 
  END ) is not null
and visibility.ty_visibility !='Private'
then 1
else 0
END as PARTICIPANT,

Now the problem is it's not checking the entire condition,it's stopping at the first line itself(ty.type='Catalog'). Even when ty_visibility is equal to Private it's selecting the value as 1 instead of 0

Can someone point where I went wrong?

Specify or condition correctly. Hope following will work correct.

CASE
when ty.type ='Catalog'
and (zs.name='Aries'
or zs.name='Leo')
AND ( CASE 
when actual_finish_date is not null
then actual_finish_date
when updated_finish_date is not null
then updated_finish_date
else baseline_finish_date 
  END ) is not null
and visibility.ty_visibility !='Private'
then 1
else 0
END as PARTICIPANT,

Try this.

You need to add parentheses to get 'Catalog' and one of the names:

ty.type ='Catalog' and (zs.name='Aries' or zs.name='Leo')

Or switch to IN:

ty.type ='Catalog' and zs.name IN ('Aries', 'Leo')

You can also use COALESCE to simplify the is not null tests:

coalesce(actual_finish_date , updated_finish_date , baseline_finish_date ) is not null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM