如何删除SQL表里的从复数据

Franky668 2005-07-12 11:14:38
我有一个SQL表,他有五列
分别是:型号,厂家,数量,年份,封装

我想当“型号”相同的时候删除掉相同的数据,只留下一个


请问得怎么写
...全文
129 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlp321002 2005-07-12
  • 打赏
  • 举报
回复
--Blog:http://blog.csdn.net/zlp321002/archive/2005/01/12/249819.aspx 有个例子
sheercony 2005-07-12
  • 打赏
  • 举报
回复
--取不重复型号经理---------------------------------------------------------------------------
if exists(select name from sysobjects where name='SBU_型号与型号经理对照表_temp')
drop table SBU_型号与型号经理对照表_temp

declare @cpbm varchar(11)
select top 0 * into SBU_型号与型号经理对照表_temp from SBU_型号与型号经理对照表
declare cur_temp cursor for
select distinct 产品编码 from SBU_型号与型号经理对照表
where 产品编码 in
(select distinct 产品编码
from SBU_型号与型号经理对照表
group by 产品编码
having count(*)>=2)
open cur_temp
fetch next from cur_temp into @cpbm
while @@fetch_status=0
begin

insert into SBU_型号与型号经理对照表_temp
select top 1 * from SBU_型号与型号经理对照表 where 产品编码=@cpbm

delete from SBU_型号与型号经理对照表 where 产品编码=@cpbm

fetch next from cur_temp into @cpbm
end
close cur_temp
DEALLOCATE cur_temp

大概的意思,如果重复的数据很少,效率比较高,反之,效率比较低,改改可以用
Franky668 2005-07-12
  • 打赏
  • 举报
回复
用不了哦
zlp321002 2005-07-12
  • 打赏
  • 举报
回复
--随机查询出来的
select 厂家,数量,年份,封装
,(select top 1 型号 from 表 where 型号 = d.型号 order by newid()) as 型号
--随机取c列的值,也可以用Max(型号),Min(型号), AVG(型号)
from 表 d
group by 型号
Franky668 2005-07-12
  • 打赏
  • 举报
回复
我有一个SQL表,他有五列
分别是:型号,厂家,数量,年份,封装

我想当“型号”相同的时候删除掉相同的数据,只留下一个


请问得怎么写
请帮帮忙

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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