您可以通过将正则表达式调用推入内联视图或 CTE 来简化一点,这样您就不必重复它们:
select id, day_ph, eve_ph, mob_ph,
case when length(day_ph_cln) = 10 and day_ph_cln != '9999999999'
then 0 else 1 end as invalid_day_ph,
case when length(eve_ph_cln) = 10 and eve_ph_cln != '9999999999'
then 0 else 1 end as invalid_eve_ph,
case when length(mob_ph_cln) = 10 and mob_ph_cln != '9999999999'
then 0 else 1 end as invalid_mob_ph
from (
select id, day_ph, eve_ph, mob_ph,
regexp_replace(day_ph, '[^[:digit:]'''']') as day_ph_cln,
regexp_replace(eve_ph, '[^[:digit:]'''']') as eve_ph_cln,
regexp_replace(mob_ph, '[^[:digit:]'''']') as mob_ph_cln
from your_table
);
trim() 似乎没有做任何事情,所以我把它省略了 - 正则表达式已经删除了所有空格,因为它们不是数字(或单引号......我已经离开了模式完好无损,但不确定它是否有意义......)
如果您想要一个最终标志来显示是否所有其他标志都无效,那么您可以使用least(),如果其他三个标志中的任何一个为零,它将给您零,否则为 1。
select id, day_ph, eve_ph, mob_ph,
case when length(day_ph_cln) = 10 and day_ph_cln != '9999999999'
then 0 else 1 end as invalid_day_ph,
case when length(eve_ph_cln) = 10 and eve_ph_cln != '9999999999'
then 0 else 1 end as invalid_eve_ph,
case when length(mob_ph_cln) = 10 and mob_ph_cln != '9999999999'
then 0 else 1 end as invalid_mob_ph,
least(
case when length(day_ph_cln) = 10 and day_ph_cln != '9999999999'
then 0 else 1 end,
case when length(eve_ph_cln) = 10 and eve_ph_cln != '9999999999'
then 0 else 1 end,
case when length(mob_ph_cln) = 10 and mob_ph_cln != '9999999999'
then 0 else 1 end
) as all_invalid
from (
select id, day_ph, eve_ph, mob_ph,
regexp_replace(day_ph, '[^[:digit:]'''']') as day_ph_cln,
regexp_replace(eve_ph, '[^[:digit:]'''']') as eve_ph_cln,
regexp_replace(mob_ph, '[^[:digit:]'''']') as mob_ph_cln
from your_table
);
或使用其他级别的子查询(或 CTE)来删除重复:
select id, day_ph, eve_ph, mob_ph,
invalid_day_ph, invalid_eve_ph, invalid_mob_ph,
least(invalid_day_ph, invalid_eve_ph, invalid_mob_ph) as all_invalid
from (
select id, day_ph, eve_ph, mob_ph,
case when length(day_ph_cln) = 10 and day_ph_cln != '9999999999'
then 0 else 1 end as invalid_day_ph,
case when length(eve_ph_cln) = 10 and eve_ph_cln != '9999999999'
then 0 else 1 end as invalid_eve_ph,
case when length(mob_ph_cln) = 10 and mob_ph_cln != '9999999999'
then 0 else 1 end as invalid_mob_ph
from (
select id, day_ph, eve_ph, mob_ph,
regexp_replace(day_ph, '[^[:digit:]'''']') as day_ph_cln,
regexp_replace(eve_ph, '[^[:digit:]'''']') as eve_ph_cln,
regexp_replace(mob_ph, '[^[:digit:]'''']') as mob_ph_cln
from your_table
)
);
如果您只想要最后一个标志,那么您可以组合大小写表达式:
select id, day_ph, eve_ph, mob_ph,
case when (length(day_ph_cln) = 10 and day_ph_cln != '9999999999')
or (length(eve_ph_cln) = 10 and eve_ph_cln != '9999999999')
or (length(mob_ph_cln) = 10 and mob_ph_cln != '9999999999')
then 0 else 1 end as all_invalid
from (
select id, day_ph, eve_ph, mob_ph,
regexp_replace(day_ph, '[^[:digit:]'''']') as day_ph_cln,
regexp_replace(eve_ph, '[^[:digit:]'''']') as eve_ph_cln,
regexp_replace(mob_ph, '[^[:digit:]'''']') as mob_ph_cln
from your_table
);
您没有提供任何样本数据来验证这是否提供了您想要的结果;但是here's a db<>fiddle 有一些非常基本的虚构数据。