得到错误“错误:表“ test1”的缺少从句条目

时间:2019-10-10 20:08:35

标签: database postgresql postgresql-9.5

在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 )

1 个答案:

答案 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 )