在teradata 16.20中,有没有一种方法可以从两个CTE进行更新或合并?

kx7yvsdv  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(431)

在teradata 16.20中,有没有一种方法可以更新或合并两个CTE?
例如,在mssql中,我们有第一个cte,第二个cte使用第一个cte,然后更新:

with CTE1 as (
 select alpha, beta
 from someTable a
 join otherTable b on a.aleph = b.aleph
), CTE2 as (
 select beta, gamma
 from CTE1 c
 join anotherTable d on c.alpha = d.alpha
)
update u
set u.gamma = e.gamma
from updateTable u
join CTE2 e on u.beta = e.beta;

在teradata 16.20中,这当然适用于一个cte,如下所示:

merge into mydb.mytable
using (
 select alpha, beta
 from someTable a
 join otherTable b on a.aleph = b.aleph
) as CTE (alpha, beta)
on mytable.alpha = CTE.alpha
when matched then update
set beta = CTE.beta;

有没有办法用两个或更多的CTE来实现这一点?

mcvgt66p

mcvgt66p1#

不能在派生表中使用with(cte)(这是 USING 合同条款 MERGE 语句),但可以使用嵌套的派生表:

merge into mydb.mytable u
using (
 select beta, gamma
 from (
   select alpha, beta
   from someTable a
   join otherTable b on a.aleph = b.aleph
 ) CTE1      
 join anotherTable d on CTE1.alpha = d.alpha
) CTE2 
on u.beta = CTE2.beta
when matched then update
set gamma = CTE2.gamma;

或者如果 MERGE 不适用于(例如联接 predicate 不包括所有主索引列)使用联接的相同方法 UPDATE :

UPDATE u FROM mydb.mytable u, 
(
 select beta, gamma
 from (
   select alpha, beta
   from someTable a
   join otherTable b on a.aleph = b.aleph
 ) CTE1      
 join anotherTable d on CTE1.alpha = d.alpha
) CTE2 
set gamma = CTE2.gamma
WHERE u.beta = CTE2.beta;

相关问题