2009-06-25 18 views
0

私は主キーとしていくつかの整数を持つテーブルを持っています。それらの1つはカウンタです。私は鍵からカウンターを取り除く必要があります。キーを変更した後にデータを修正する方法

キーからカウンタを削除すると、多くの行が重複します(カウンタの値は異なりますが、他のすべてのキー要素は同じです)。

すべての重複を削除し、最も高いカウンタ値を持つ行のみを残すクエリが必要です。どんな助けもありがとうございます。

答えて

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:あなたは主キーからCを取り外す前を削除する必要があります。

+0

これは、1回の出現ですべての行を削除しませんか? – Nir

+0

作業例を含むように変更:selectはc = 1の行のみを返します。 – l0b0

関連する問題