Sql 联接定义中的Teradata通配符

Sql 联接定义中的Teradata通配符,sql,teradata,Sql,Teradata,我加入了以下表格: select a.*, b.col4, b.col5 from table a inner join table b on a.col2=b.col2 and a.col3=b.col3 在b.col2中,b.col3可能是值“*”,这应该是通配符,也就是说,在这种情况下,我们可以将b.col2的值与a.col2的任何值连接起来,或将b.col3的值与a.col3的任何值连接起来 您能帮我定义一下吗?听起来您有一个默认值。一种方法是多重比较: select a.*,

我加入了以下表格:

select a.*, b.col4, b.col5 from table a
inner join table b
on  a.col2=b.col2
and a.col3=b.col3
在b.col2中,b.col3可能是值“*”,这应该是通配符,也就是说,在这种情况下,我们可以将b.col2的值与a.col2的任何值连接起来,或将b.col3的值与a.col3的任何值连接起来


您能帮我定义一下吗?

听起来您有一个默认值。一种方法是多重比较:

select a.*,
       coalesce(b.col4, bdef3.col4, bdef2.col4, bdef.col4) as col4, b.col5
       coalesce(b.col5, bdef3.col5, bdef2.col5, bdef.col5) as col5
from tablea a left join
     tableb b
     on b.col2 = a.col2 and b.col3 = a.col3 left join
     tableb bdef3
     on b.col2 = a.col2 and b.col3 = '*' left join
     tableb bdef2
     on b.col2 = '*' and b.col3 = a.col3 left join
     tableb bdef
     on b.col2 = '*' and b.col3 = '*';
如果要保证某些匹配,可能需要一个
where
子句:

where (b.col2 is not null or bdef3.col2 is not null or bdef2.col2 is not null or bdef.col2 is not null)
我认为上述方法更有效,但您可以更简洁地表示为:

select a.*, b.col4, b.col5
from tablea a left join
     tableb b
     on (b.col2 = a.col2 or b.col2 = '*') and
        (b.col3 = a.col3 or b.col3 = '*')
qualify 1 = row_number() over (partition by a.id order by (case when b.col2 = '*' then 2 else 1 end), (case when b.col3 = '*' then 2, else 1 end))