在postgres中遇到重复的情况下,我尝试使用约束来更新行,但出现错误“错误:表“ test1”的FROM子句条目丢失“
INSERT INTO wfo_data_aggregation.test2
SELECT *
FROM wfo_data_aggregation.test1
ON CONFLICT ON CONSTRAINT test2_un
do update set inbound_handled_seconds = wfo_data_aggregation.test1.inbound_handled_seconds
AND contacts_handled_inbound = wfo_data_aggregation.test1.contacts_handled_inbound
AND outbound_handled_seconds = wfo_data_aggregation.test1.outbound_handled_seconds
AND contacts_handled_outbound = wfo_data_aggregation.test1.contacts_handled_outbound
WHERE NOT EXISTS (SELECT *
FROM wfo_data_aggregation.test2
WHERE start_time = wfo_data_aggregation.test1.start_time
AND end_time = wfo_data_aggregation.test1.end_time
AND site_name = wfo_data_aggregation.test1.site_name
AND skill_id = wfo_data_aggregation.test1.skill_id
and site_id = wfo_data_aggregation.test1.site_id
AND channel = wfo_data_aggregation.test1.channel
AND inbound_handled_seconds = wfo_data_aggregation.test1.inbound_handled_seconds
AND contacts_handled_inbound = wfo_data_aggregation.test1.contacts_handled_inbound
AND outbound_handled_seconds = wfo_data_aggregation.test1.outbound_handled_seconds
AND contacts_handled_outbound = wfo_data_aggregation.test1.contacts_handled_outbound
AND tenure = wfo_data_aggregation.test1.tenure )
答案 0 :(得分:0)
DO UPDATE
子句使用特殊语法EXCLUDED
来引用排除的插入:
INSERT INTO wfo_data_aggregation.test2
SELECT *
FROM wfo_data_aggregation.test1
ON CONFLICT ON CONSTRAINT test2_un
do update set inbound_handled_seconds = EXCLUDED.inbound_handled_seconds
AND contacts_handled_inbound = EXCLUDED.contacts_handled_inbound
AND outbound_handled_seconds = EXCLUDED.test1.outbound_handled_seconds
AND contacts_handled_outbound = EXCLUDED.test1.contacts_handled_outbound
WHERE NOT EXISTS (SELECT *
FROM wfo_data_aggregation.test2 t2
WHERE t2.start_time = test1.start_time
AND t2.end_time = test1.end_time
AND t2.site_name = test1.site_name
AND t2.skill_id = test1.skill_id
and t2.site_id = test1.site_id
AND t2.channel = test1.channel
AND t2.inbound_handled_seconds = test1.inbound_handled_seconds
AND t2.contacts_handled_inbound = test1.contacts_handled_inbound
AND t2.outbound_handled_seconds = test1.outbound_handled_seconds
AND t2.contacts_handled_outbound = test1.contacts_handled_outbound
AND t2.tenure = test1.tenure )