删除相同的数据如何写?

hyit2004 2005-07-29 10:01:35
create table #test
(id int,
badge varchar(20),
num decimal(9,2)
)
insert into #test
select 1,0025,92.5
union all
select 2,0024,92.3
union all
select 3,0023,92.2
union all
select 3,0023,92.2
union all
select 4,0021,92.1
union all
select 5,0020,92.0
union all
select 6,0028,91.1

select * from #test
drop table #test

id badge num
----------- -------------------- -----------
1 25 92.50
2 24 92.30
3 23 92.20
3 23 92.20
4 21 92.10
5 20 92.00
6 28 91.10

其中ID是3的有两条相同的记录,我如果删除其中一条,变成
id badge num
----------- -------------------- -----------
1 25 92.50
2 24 92.30
3 23 92.20
4 21 92.10
5 20 92.00
6 28 91.10
此题之意是只有有两条及两条以上相同,只保留一条,建议只在#test表里删除操作!望赐教!
...全文
148 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyit2004 2005-10-20
  • 打赏
  • 举报
回复
^_^ 以前我写过select就可以删除,关键是我想不起来,不管怎么样,谢谢大家
vivianfdlpw 2005-08-01
  • 打赏
  • 举报
回复
select id,[badge]=min(badge),num=min(num) into # from #test group by id
truncate table #test
insert #test select * from #

--查看
select * from #test
Yisa 2005-08-01
  • 打赏
  • 举报
回复
这样就OK了.

select distinct * into #t from #test
go
truncate table #test
go
insert into #test select * from #t
go
select * from #test
go


vivianfdlpw 2005-08-01
  • 打赏
  • 举报
回复
select [a]=identity(int,1,1),* into # from #test
truncate table #test
insert #test
select id,badge,num from # t
where not exists(select 1 from # where id=t.id and a>t.a)


--查看
select * from #test
hyit2004 2005-08-01
  • 打赏
  • 举报
回复
这个重复数据不能确定是哪一个,我希望能把重复的筛选出来参数,但是原来数据里面还要保留一个!谢谢!
geniusqing 2005-07-29
  • 打赏
  • 举报
回复
select * into #aa from #test group by id,badge,num
delete from #test
insert into #test
select * from #aa
select * from #test
xueguang 2005-07-29
  • 打赏
  • 举报
回复
如果只是个别记录,也可以这样
set rowcount 1
delete #test where id=3
set rowcount 0
xueguang 2005-07-29
  • 打赏
  • 举报
回复
select id,min(badge) badge,min(num) num into #1 from #test group by id
truncate table #test
insert #test select * from #1
tangqijun199 2005-07-29
  • 打赏
  • 举报
回复
select * into #3 from #test where id=3

delete #test where id=3

insert into #test select top 1 * from #3

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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