简体   繁体   English

SQL - 在多列中搜索多个值以创建标志

[英]SQL - Search multiple values across multiple columns to create a flag

I am trying to search for multiple values across multiple columns so I can create an inclusion criteria flag, but can't figure out the best way.我正在尝试跨多列搜索多个值,以便我可以创建包含条件标志,但无法找出最佳方法。 I need to create a flag, so I am trying to not have to use a where statement我需要创建一个标志,所以我试图不必使用where语句

I have this in my code now for only one value and it works:我现在在我的代码中只有一个值,它可以工作:

when 'a' in (col1, col2, col3, col4) 
then 1 
else 0 
end as col_a

Would like to do something like, but can't figure it out:想做类似的事情,但无法弄清楚:

when 'a', 'b', 'c' in (col1, col2, col3, col4) 
then 1 
else 0 
end as col_all

Any help is appreciated!任何帮助表示赞赏! Thanks!谢谢!

Is this what you want?这是你想要的吗?

(case when 'a' in (col1, col2, col3, col4) and
           'b' in (col1, col2, col3, col4) and
           'c' in (col1, col2, col3, col4) 
      then 1 else 0
 end) as col_all

It is a little unclear if you want all to match or any to match.是否要全部匹配或任何匹配不太清楚。 If the latter, use or rather than and .如果是后者,请使用or而不是and

Try this尝试这个

case array_except(array['a','b','c'],array[col1,col2,col3,col4]) 
when array[] 
then 1 
else 0 
end as col_all 

在此处输入图片说明

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

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