![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Task : Delete rows from one table and insert into another CREATE TABLE Archive LIKE Inventory; WITH del(Item, Quantity, InvDate) AS (SELECT Item, Quantity, InvDate FROM OLD TABLE ( DELETE FROM (SELECT Item, Quantity, InvDate, row_number() OVER(PARTITION BY Item ORDER BY InvDate DESC) AS rn FROM Inventory) WHERE rn > 1)), ins(x) AS (SELECT 1 FROM NEW TABLE(INSERT INTO SELECT * FROM del)) SELECT COUNT(1) FROM ins; Hi I found the above sql in presentation named 'SQl on fire' amazing one, and has a lot to do in set processing. But I want to know why this works and not the one below insert into newtab (select * from old table (delete from very old table)) Is it because in the above query, the insert will try to work before the deletion, and old table cursor population ? By that I can agree with the above sql, first delete, then cursor then insert into new table. Or is there any other reason? I didnt get any notes on the presentation and hence this question. When you check the db2exfmt for the working query you will (should at |
#3
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |