2016-11-29 49 views
0

Image contains two tables, Now I want to delete duplicate table from table 1 with comparing data with table 2通過比較數據表2

像刪除從表1中的重複數據,我想刪除包含234一排,只保留另一行,依此類推。

**注:我正在使用IBM DB2作爲數據庫。

+1

http://stackoverflow.com/問題/ 595433/how-can-i-compare-two-tables-and-delete-the-duplicate-rows-in-sql –

回答

-1

刪除頂部(SELECT COUNT(a.Policy)-1從表1 x,其中x.policy在(選擇從表2不同的政策))從表1

+0

請格式化您的代碼並在幾行描述您的答案,謝謝,格式爲代碼展開您的代碼左邊有4個空格 – hmmftg

0
delete from table1 f0 
where rrn(f0) in 
(
    select f3.rw from (
        select rrn(f1) rw, 
        rownumber() over(partition by Policy) rang 
        from table1 f1 inner join table2 f2 on f1.policy=f2.policy 
        ) f3 
    where f3.rang=1 
)