在sql查询中的c> 1中从重复记录中插入一行

时间:2016-02-09 16:36:12

标签: sql oracle

我有一张表i_table。我正在从x_ass_table into i_ass_table.

插入数据

i_ass_table针对ASSIGNMENT_NUMBER,EFFECTIVE_START_DATE,EFFECTIVE_END_DATE,EFFECTIVE_LATEST_CHANGE创建了一个索引 现在为了避免将重复数据从a_table插入到i_table中,我使用了count partition by:

CREATE UNIQUE INDEX i_ass_table_pk ON 
i_ass_table(ASSIGNMENT_NUMBER,EFFECTIVE_START_DATE,EFFECTIVE_END_DATE,EFFECTIVE_LATEST_CHANGE)



insert into i_ass_table

SELECT *
FROM   (SELECT *,
               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

这是正确插入数据。

现在对于c> 1的数据,我想只插入一行但要插入i_Ass_table的数据。 如何从c> 1插入一行到i_ass_table?

2 个答案:

答案 0 :(得分:0)

您可以使用以下where子句

^| sqlplus / @kiva_extract $assessorYear" | Out-File H:\kivaCommands.txt -Encoding Unicode

答案 1 :(得分:0)

select * from (select X.*,
           ROW_NUMBER() OVER (PARTITION BY assignment_number, 
                                      effective_start_date,
                                      effective_end_date,
                                      effective_latest_change order by assignment_number) as RN 
           from  X_ass_table X)
           where RN=1)