只是一般性问题,任何人都可以帮助如何对联合数据执行左连接。我有查询,我正在根据几个条件进行数据联合(使用表1和表2),现在我需要与第三个表(表5)执行左外连接,所以我正在寻求帮助如何做到这一点。只需示例查询就可以了。
我在小提琴上创建了一个场景,我使用表 1 和表 2 基于几个条件进行了联合。https://dbfiddle.uk/WN6uC5MH 现在我需要使用联合的输出并对第三个表(即表 5)执行左连接。因此,我需要对表 5 的字段 6 与联合数据的输出字段 1 执行左连接(如果匹配),然后在表 5 中插入行除字段 9 外,所有值都将保持不变,字段 9 是我们从联合输出数据(字段 3)中获得的: 输出将如下所示:
'j','abc','def','xyz_inc'
'j','abc','def','5_abc'
'j','abc','def','6_abc'
translate()
replace()
两次。您还可以合并 translate()
调用,而不是嵌套这些调用。您可以直接regexp_split_to_table()
unnest(string_to_array())
。正如
with your_big_union as (
select field1,field2,string_agg(distinct fld,',') field3
from(
select *
from(
select field1,field2,fld
from table1 t1
left join (
select * from table2
cross join regexp_split_to_table(trim(translate(field4,'{}','')),',') fld
)t2
on concat(t1.field2,';') similar to concat('%',t2.field3,'[();]%')
)x
)b
where field2 like '%fn@_%' escape '@' and fld is not null
group by field1,field2
union
(with cte as (
select field1,field2
,''''
||translate( btrim(field2)
,E'\n"'''
,'' )
||'''' as edited_field2
from table1)
,cte2 as (
select *,t.spotted_table as spotted_target,s.spotted_table as spotted_source
from cte
left join regexp_matches(
edited_field2
,'(?:UPDATE|INTO)(?:\s+ONLY)?\s+([[:alpha:]_]+[\.\w]*|"[^"]+"(?:\."[^"]+")?)'
,'gi'
) with ordinality as target_matches(hits,n1) on true
left join unnest(target_matches.hits) with ordinality as t(spotted_table,n2) on true
left join regexp_matches(
edited_field2
,'(?:FROM|JOIN|USING|TABLE)(?:\s+ONLY)?\s+([[:alpha:]_]+[\.\w]*|"[^"]+"(?:\."[^"]+")?)'
,'gi'
) with ordinality as source_matches(hits,n1) on true
left join unnest(source_matches.hits) with ordinality as s(spotted_table,n2) on true)
select field1
,string_agg(distinct spotted_target,',') as spotted_targets
-- ,string_agg(distinct spotted_source,',') as spotted_sources
,edited_field2
from cte2 where edited_field2 not like '%fn@_%' escape '@' and spotted_target is not null
and coalesce(split_part(spotted_target,'.',1) !~* '_inc"?$',true)
and coalesce(split_part(spotted_source,'.',1) !~* '_inc"?$',true)
group by field1,edited_field2
order by field1))
select *
from table5 left join your_big_union
on lower(your_big_union.field1) = table5.field6;