求急,删除重复的id号记录,只保留一条记录?在线等代!

chengxing 2002-08-30 10:33:32
表的结构如下:

ID NewCode PartName Kind
---------------------------------------------
14 A332-PCUBOCSR-HT HT-8008SR ASSM
14 A332-PCUBOCSR-HT HT-8008SR ASSM
15 A332-PCUBOCSR HT-8008SF DIP
15 A332-PCUBOCSR HT-8008SF DIP

?1.删除整个表中重复的ID号记录,只保留一条记录。该如何实现!谢谢!
例如:id重复的14记录有两条,现在只保留一条在表中,该如可实现?

求急!在线等代!谢谢!
...全文
73 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
AWP365 2002-08-30
  • 打赏
  • 举报
回复
加入一个自动增加的列(identity)id_1
delete from tablename where id_1 not in (select min(id_1) from tablename group by id )
given 2002-08-30
  • 打赏
  • 举报
回复
上面有错:
更正如下:

select ID,min(NewCode),min(PartName),min(Kind) into #temp from table group by id having count(id)>=2
insert into #temp select * from table where id not in (select id from #temp)
delete from table
insert into table select * from #temp
drop table #temp
given 2002-08-30
  • 打赏
  • 举报
回复
select ID,min(NewCode),min(PartName),min(Kind) into #temp from table group by xtype having count(xtype)>=2

delete from table
insert into table select * from #temp
drop table #temp
Chiff 2002-08-30
  • 打赏
  • 举报
回复
同意given(andy)
chengxing 2002-08-30
  • 打赏
  • 举报
回复
to Yang_(扬帆破浪):你好!我这里就是要删除id号相同的,只保留一条,别的字段相同不管,表中只允许id号没有相同的,并且仅有一条.
given 2002-08-30
  • 打赏
  • 举报
回复
select distinct * into #temp from table
delete from table
insert into table select * from #temp
drop table #temp
Yang_ 2002-08-30
  • 打赏
  • 举报
回复
你的数据完全一样?

SELECT ID,MIN(NewCode) AS NewCode,MIN(PartName) AS PartName,MIN(Kind) AS Kind
INTO TEMPTABLE
FROM TABLENAME

TRUNCATE TABLE TABLENAME

INSERT TABLENAME
SELECT * FROM TEMPTABLE

DROP TABLE TEMPTABLE

gzhughie 2002-08-30
  • 打赏
  • 举报
回复
Yang_(扬帆破浪) 的方法好,只是有点错误 应该是

SELECT ID,MIN(NewCode) AS NewCode,MIN(PartName) AS PartName,MIN(Kind) AS Kind
INTO TEMPTABLE
FROM TABLENAME
GROUP BY ID

TRUNCATE TABLE TABLENAME

INSERT TABLENAME
SELECT * FROM TEMPTABLE

DROP TABLE TEMPTABLE

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧