2009-06-25 32 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