简体   繁体   English

如何在CASE语句中编写多个条件(和和/或组合)?

[英]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'). 现在的问题是它没有检查整个条件,而是在第一行本身停止(ty.type ='Catalog')。 Even when ty_visibility is equal to Private it's selecting the value as 1 instead of 0 即使ty_visibility等于Private,它也会将值选择为1而不是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: 或切换至IN:

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

You can also use COALESCE to simplify the is not null tests: 您还可以使用COALESCE简化is not null测试:

coalesce(actual_finish_date , updated_finish_date , baseline_finish_date ) is not null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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