日期:2014-05-17  浏览次数:20676 次

如何删除数据表中重复列?
表T中字段A有重复的,也就是>1,现在想只保留一列,其中字段B是唯一的,可以比较大小,留B字段大的一条。

------解决方案--------------------
要删除某字段,要看数据库是否支持,有不少数据库不支持删除字段。

alter table [table_name] drop column [column_name]
------解决方案--------------------
oracle版本
SQL code

create table test (col_a number,col_b number);
insert into test values(1,1);
insert into test values(1,2);
insert into test values(1,3);
insert into test values(2,4);
insert into test values(2,5);
select AA.*
  from test AA,
       (select col_a, max(col_b) col_b
          from test
         where col_a in
               (select col_a
                  from (select col_a, count(distinct col_b)
                          from test
                         group by col_a
                        having count(distinct col_b) > 1) temp)
         group by col_a) tt
 where AA.col_a = tt.col_a
   and AA.col_b < tt.col_b;

------解决方案--------------------
看来又理解错了,楼主是要删除重复记录。总是被你的删除重复列给弄晕了,如下:
SQL code

Delete From T Where (a, b) NOT IN (Select A, max(B) as B From T Group By A)