根据条件将数据从 table 移动到多个 table
Moving data from table to multiple tables depending on condition
我正在尝试将数据从一个 table 移动到另外两个 table,具体取决于是否满足特定条件。
这就是我正在尝试的:
WITH moved_rows AS (
DELETE FROM rac_temp_import
WHERE depo IN ('0281752') AND tura IN ('026','094','097')
RETURNING *, (CASE WHEN parcelno IN(
SELECT parcelno FROM rac_import
UNION
SELECT parcelno FROM rac_dupli
) THEN true ELSE false END) AS dupli
)
INSERT INTO rac_dupli
SELECT FROM moved_rows WHERE dupli = true
INSERT INTO rac_import
SELECT FROM moved_row WHERE dupli = false
但据我所知,在 WITH
之后只能存在一个 INSERT
语句,我尝试在之后使用 CASE
但这也没有用。
使第一个 insert
成为 CTE:
with moved_rows as (
delete from rac_temp_import rti
where depo in ('0281752') and tura in ('026','094','097')
returning *,
exists (
select parcelno
from rac_import
where parcelno = rti.parcelno
) or
exists (
select parcelno
from rac_dupli
where parcelno = rti.parcelno
) as dupli
), i as (
insert into rac_dupli
select
from moved_rows
where dupli = true
)
insert into rac_import
select
from moved_rows
where dupli = false
exists
可能比 case in
快
我正在尝试将数据从一个 table 移动到另外两个 table,具体取决于是否满足特定条件。
这就是我正在尝试的:
WITH moved_rows AS (
DELETE FROM rac_temp_import
WHERE depo IN ('0281752') AND tura IN ('026','094','097')
RETURNING *, (CASE WHEN parcelno IN(
SELECT parcelno FROM rac_import
UNION
SELECT parcelno FROM rac_dupli
) THEN true ELSE false END) AS dupli
)
INSERT INTO rac_dupli
SELECT FROM moved_rows WHERE dupli = true
INSERT INTO rac_import
SELECT FROM moved_row WHERE dupli = false
但据我所知,在 WITH
之后只能存在一个 INSERT
语句,我尝试在之后使用 CASE
但这也没有用。
使第一个 insert
成为 CTE:
with moved_rows as (
delete from rac_temp_import rti
where depo in ('0281752') and tura in ('026','094','097')
returning *,
exists (
select parcelno
from rac_import
where parcelno = rti.parcelno
) or
exists (
select parcelno
from rac_dupli
where parcelno = rti.parcelno
) as dupli
), i as (
insert into rac_dupli
select
from moved_rows
where dupli = true
)
insert into rac_import
select
from moved_rows
where dupli = false
exists
可能比 case in