我在嘗試,如果我的客戶表包含以下從我的表,例如刪除重複行:在多個查詢中使用「with」語句?
first_name last_name email
fred wilford [email protected]
fred wilford [email protected]
Damian Jones [email protected]
理想的結果應該是以下幾點:
first_name last_name email
fred wilford [email protected]
Damian Jones [email protected]
這應該是相當簡單在刪除主表中的重複項之前,使用包含重複行的中間表執行操作,並最後將中間表中的所有行插入主表中。不過,我寧願刪除中間表,只是使用類似的語句。 考慮下面的例子:
with dups as
(
select name,last_name,email from customer group by 1,2,3 having
count(*) > 1
)
delete from customer
using
(
select name,last_name,email from customer group by 1,2,3 having
count(*) > 1
)b
where b.name = customer.name;
insert into customer
(
select name,last_name,email from dups
)
麻煩的是最終的INSERT語句未能按「DUP的」不被認可。有沒有辦法來解決這個問題?在此先感謝
通用表防爆壓力只存在於主查詢中 - 您需要使用臨時表或表變量在多個查詢中使用它。 –