在sql中使用partition by将非重复行插入到表中

时间:2016-01-15 09:01:43

标签: sql oracle

有两个表x_ass_tablei_ass_table

我将来自x_ass_table的数据与1128条记录一起插入i_ass_table。为了避免唯一约束错误,我使用以下查询:

     insert assignment_number, 
            effective_start_date,
            effective_end_date,
            effective_latest_change,
            configuration_no 
     into i_ass_table
          select effective_start_date,
                 effective_end_date,
                 effective_latest_change,
                 'XX_FUS' 
      FROM     (SELECT x_ass_table.*,
                       COUNT() OVER (PARTITION BY assignment_number, 
                                                  effective_start_date,
                                                  effective_end_date,
                                                  effective_latest_change) AS c
                FROM   x_ass_table) t
    where c=1;

我仍然遇到唯一约束错误。虽然我限制了assignment_number, effective_start_date,effective_end_date,effective_latest_change列,这些列是唯一的键列。

1 个答案:

答案 0 :(得分:0)

我认为您正在检查x_ass_table中存在的重复记录,但您还应检查i_ass_tablex_ass_table中是否存在相同的记录。这也可能会产生唯一的约束错误。

    select *  
      from x_ass_table x,  
           i_ass_table i  
    where i.assignment_number = x.assignment_number  
      and i.effective_start_date = x.effective_start_date  
      and i.effective_end_date = x.effective_end_date  
      and i.effective_latest_change = x.effective_latest_change;