2009-06-25 77 views
0

我有一個表中有幾個整數作爲主鍵。其中之一是一個櫃檯。我需要從鑰匙中取出計數器。更改密鑰後如何修復數據

從鍵中移除計數器會使許多行重複(具有不同的計數器值,但所有其他鍵元素相同)。

我需要一個查詢,將刪除所有重複項,只保留具有最高計數器值的行。任何幫助表示讚賞。

回答

0

如何:

create table foo (
    a number, 
    b number, 
    c number, 
    constraint pk_foo primary key (a, b, c) 
); 
insert into foo (a, b, c) values (0, 0, 0); 
insert into foo (a, b, c) values (0, 0, 1); 
insert into foo (a, b, c) values (0, 1, 0); 
insert into foo (a, b, c) values (0, 1, 1); 
insert into foo (a, b, c) values (1, 0, 0); 
insert into foo (a, b, c) values (1, 0, 1); 
insert into foo (a, b, c) values (1, 1, 0); 
insert into foo (a, b, c) values (1, 1, 1); 
delete from foo t1 
where t1.c not in (
     select max(t2.c) 
      from foo t2 
      group by a, b 
     ) 
; 
select * from foo; 

PS:你必須從主鍵刪除Ç之前刪除

+0

請問這不會刪除所有行的一次? – Nir 2009-06-25 11:21:10