По сути, вы ищете full join
с условными логами c в предложении from
:
insert into table4
select
t1.col1,
t1.col2,
t1.col3,
t2.col6,
t3.col1,
t3.col7,
case
when t2.col7 is null then 'insert'
when t1.col1 is null then 'delete'
else 'update'
end action
from table1 t1
full join table2 t2 on t1.col1 = t2.col7 and t1.col2 = t2.col8 and t1.col3 = t2.col9
full join table3 t3 on t1.col1 = t3.col9 and t1.col2 = t2.col11 and t1.col3 = t3.col7
Не очень понятно, что вы хотите сделать с table3
. Возможно, вы хотите left join
вместо full join
с условиями, которые проверяются в обеих предыдущих таблицах, например:
left join table t3
on t3.col9 = coalesce(t1.col1, t2.col7)
and t3.col11 = coalesce(t1.col2, t2.col8)
and t3.col7 = coalesce(t1.col3, t2.col9)