去除某几个字段联合重复的SQL

programmer_lee 2011-01-13 07:45:36
需求,表tab有字段 id,col1,name,col2,col3..有如下记录:

id col1 name col2 col3
-------------------------------
1 .... 中国 ..... .....
2 .... 韩国 ..... .....
1 .... 中国 ..... .....
3 .... 印度 ..... .....
4 .... 德国 ..... .....
2 .... 韩国 ..... .....
2 .... 韩国 ..... .....

等等数据...其中col1,col2,col3这几个字段无关紧要..
如果id+name有重复的话,比如1+中国、2+韩国..希望能够删除重复并只保留其中一条(id最小的)记录.
...全文
183 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
快溜 2011-01-13
  • 打赏
  • 举报
回复
delete tb where ID(唯一) in
(select ID(唯一) from tb group by id+name having count(1)>1 and ID(唯一)>min(ID(唯一))
sean_zhou 2011-01-13
  • 打赏
  • 举报
回复
DELETE TAB FROM
(SELECT MIN(ROWID) AS ROWID,ID,NAME FROM TAB
GROUP BY ID,NAME
HAVING COUNT(ROWID) > 1
) B
WHERE TAB.ID = B.ID AND TAB.NAME = B.NAME AND TAB.ROWID != B.ROWID

试试
programmer_lee 2011-01-13
  • 打赏
  • 举报
回复
各位,按照上面的都不行...是在SQL2005下面
-晴天 2011-01-13
  • 打赏
  • 举报
回复
呵呵,这样是最大的,应该这样:
create table tab(ident int identity(1,1),id int,name nvarchar(10)) --其他字段不管
insert into tab select 1,'中国'
insert into tab select 2,'韩国'
insert into tab select 1,'中国'
insert into tab select 3,'印度'
insert into tab select 4,'德国'
insert into tab select 2,'韩国'
insert into tab select 2,'韩国'
go
select * from tab
/*
ident id name
----------- ----------- ----------
1 1 中国
2 2 韩国
3 1 中国
4 3 印度
5 4 德国
6 2 韩国
7 2 韩国

*/
go
delete tab from tab a where exists(select 1 from tab where id=a.id and ident <a.ident)
go
select * from tab
/*
ident id name
----------- ----------- ----------
1 1 中国
2 2 韩国
4 3 印度
5 4 德国

(4 行受影响)

*/
go
drop table tab
-晴天 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 programmer_lee 的回复:]
应该是保留这个主键最小的一条记录
[/Quote]
设你的主键列名为 ident:
create table tab(ident int identity(1,1),id int,name nvarchar(10)) --其他字段不管
insert into tab select 1,'中国'
insert into tab select 2,'韩国'
insert into tab select 1,'中国'
insert into tab select 3,'印度'
insert into tab select 4,'德国'
insert into tab select 2,'韩国'
insert into tab select 2,'韩国'
go
select * from tab
/*
ident id name
----------- ----------- ----------
1 1 中国
2 2 韩国
3 1 中国
4 3 印度
5 4 德国
6 2 韩国
7 2 韩国

*/
go
delete tab from tab a where exists(select 1 from tab where id=a.id and ident >a.ident)
go
select * from tab
/*
ident id name
----------- ----------- ----------
3 1 中国
4 3 印度
5 4 德国
7 2 韩国

(4 行受影响)

*/
go
drop table tab
programmer_lee 2011-01-13
  • 打赏
  • 举报
回复
应该是保留这个主键最小的一条记录
programmer_lee 2011-01-13
  • 打赏
  • 举报
回复
对,表里还有一个主键,是自动递增的..
programmer_lee 2011-01-13
  • 打赏
  • 举报
回复
多谢楼上的朋友..是要按照id,name进行分组的...但是怎么样在现在的表里删除掉这些重复的数据并只保留其中之一呢..Delete语句该怎么写
叶子 2011-01-13
  • 打赏
  • 举报
回复

declare @table table (id int,col1 varchar(1),name varchar(4),col2 varchar(1),col3 varchar(1))
insert into @table
select 1,'a','中国','a','a' union all
select 2,'b','韩国','b','b' union all
select 1,'c','中国','c','c' union all
select 3,'d','印度','d','d' union all
select 4,'e','德国','e','e' union all
select 2,'f','韩国','f','f' union all
select 2,'h','韩国','h','h'

select id,MIN(col1) AS col1,[name],
MIN(col2) AS col3 ,MIN(col3) AS col3 from @table GROUP BY id,[name]
ORDER BY id

/*
id col1 name col3 col3
----------- ---- ---- ---- ----
1 a 中国 a a
2 b 韩国 b b
3 d 印度 d d
4 e 德国 e e
*/

叶子 2011-01-13
  • 打赏
  • 举报
回复
每行数据缺少唯一标识,删除的时候不好判断
gw6328 2011-01-13
  • 打赏
  • 举报
回复

select min(id),name from tb group by name

22,207

社区成员

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

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