日期:2014-05-18  浏览次数:20479 次

怎样去除某个字段重复记录?
怎样去除某个字段重复记录?表A,有姓名xm,身份证号sfzh,ab,bc等字段,其中相同的身份证号,有很多条数据,因为ab,bc等其它字段不同,现在显示所有数据身份证号sfzh相同的只显示其中一条数据,怎样写这个查询?

------解决方案--------------------
http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html
------解决方案--------------------
SQL code
select *
from (select *,row_number()over(partition by sfzh order by xm) as row from table1)t
where row=1

------解决方案--------------------
SQL code
select tid=identity(int,1,1),* into # from A

select xm,sfzh,ab,bc from # t
  where not exists(select 1 from # where sfzh=t.sfzh and tid<t.tid)

drop table #

------解决方案--------------------
SQL code

表中要有唯一ID
delete from 表
where 字段1 in (select 字段1 from 表 group by 字段1 having count(字段1)>1) and
 ID not in(select min(id) from 表 group by 字段1 having count(字段1)>1)