- You can
translate() once instead of calling replace() twice. You can also merge your translate() calls instead of nesting those.
- You can
regexp_split_to_table() directly instead of wrapping unnest(string_to_array()).
- As @JNevill pointed out, all you need to do is nest the query you have, either in a CTE or a subquery, then join to that.
Demo at db<>fiddle:
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;
- If
table5.field6 joins to field1, you'll get no matches because you uppercased your 'J' in field, while field6 holds a lowercase. I'm not sure why you expect 3 results if you only have a single 'j' on both sides of the join.
SELECT * FROM (<big union SELECT here>) as sub LEFT OUTER JOIN table3 ON sub.field1 = table3.field6;