去除重复记录的SQL

duan17 2005-02-18 09:24:51
数据:

id type flag
01 send a
01 send b
01 send c
02 send a
02 send b
01 rece a
01 rece b
结果为:
01 send a
02 send a
01 rece a

就是先按type字段分组,然后同一组中id相同的只取一条,sql 该怎么写
...全文
213 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ken2002 2005-02-18
  • 打赏
  • 举报
回复
july 2005-02-18
  • 打赏
  • 举报
回复
这个回复应足够了,俺们只能看看学习而已

回复人: zjcxc(邹建) ( ) 信誉:502 2005-2-18 10:15:13 得分: 0



--我前面是搞错了分组字段

--如果表中有主键,或者有与type联合能构成唯一键的字段,假设这个字段为 flag,则直接用下面的

select * from 表 a
where not exists(select * from 表 where type=a.type and flag<a.flag)


--如果不满足上述条件,则用临时表

select mid=identity(int),* into #t from 表
select id,type,flag
from #t a
where not exists(select * from #t where type=a.type and mid<a.mid)
drop table #t



子陌红尘 2005-02-18
  • 打赏
  • 举报
回复
--生成测试数据
select '01' as id ,'send' as type, 'a' as flag into #t
union select '01','send','b'
union select '01','send','c'
union select '02','send','a'
union select '02','send','b'
union select '01','rece','a'
union select '01','rece','b'


--执行删除操作
delete a from #t a where a.flag != (select top 1 flag from #t where id = a.id and type = a.type order by flag)


--查询结果
select * from #t


--结果:
id type flag
----------------------
01 rece a
01 send a
02 send a
NewQger 2005-02-18
  • 打赏
  • 举报
回复
既然flag字段无所谓那么可以不显示的话
SELECT id, type FROM 表 WHERE (1 = 1) GROUP BY id, type
hwcqboy 2005-02-18
  • 打赏
  • 举报
回复
select * from 表 group by id,type 就要以搞定了为什么哪么麻烦
zjcxc 元老 2005-02-18
  • 打赏
  • 举报
回复
--我前面是搞错了分组字段

--如果表中有主键,或者有与type联合能构成唯一键的字段,假设这个字段为 flag,则直接用下面的

select * from 表 a
where not exists(select * from 表 where type=a.type and flag<a.flag)


--如果不满足上述条件,则用临时表

select mid=identity(int),* into #t from 表
select id,type,flag
from #t a
where not exists(select * from #t where type=a.type and mid<a.mid)
drop table #t
NewQger 2005-02-18
  • 打赏
  • 举报
回复
zjcxc(邹建) 老兄的,漏个条件
如果表中没有主键,或者是与id配合后不重复的字段
则用临时表生成一个主键
select mid=identity(int),* into #t from 表
select * from #t a where not exists(select * from #t where id=a.id and mid<a.mid AND type = a.type)
drop table #t
dzhfly 2005-02-18
  • 打赏
  • 举报
回复
select a.id,a.type,b.flag from (select id,type from 表 group by type,id) a,
(select type,min(flag) flag from 表 group by type) b where a.type=b.type
yizhixiaozhu 2005-02-18
  • 打赏
  • 举报
回复
我只有顶了
zlp321002 2005-02-18
  • 打赏
  • 举报
回复
--看看,能否满足你的需要!
http://blog.csdn.net/zlp321002/archive/2005/01/12/249819.aspx
nosql 2005-02-18
  • 打赏
  • 举报
回复
真复杂啊。我怎么一点都不回
zjcxc 元老 2005-02-18
  • 打赏
  • 举报
回复
如果表中没有主键,或者是与id配合后不重复的字段
则用临时表生成一个主键

select mid=identity(int),* into #t from 表
select * from #t a where not exists(select * from #t where id=a.id and mid<a.mid)
drop table #t
yizhixiaozhu 2005-02-18
  • 打赏
  • 举报
回复
up
Softlee81307 2005-02-18
  • 打赏
  • 举报
回复
delete 表 from 表 a where exists(select * from 表 where id=a.id and flag<a.flag)

Softlee81307 2005-02-18
  • 打赏
  • 举报
回复
delete 表 from 表 a where not exists(select * from 表 where id=a.id and flag<a.flag)
zjcxc 元老 2005-02-18
  • 打赏
  • 举报
回复
flag 是你的表中的主键,或者是与id配合后不重复的字段
zjcxc 元老 2005-02-18
  • 打赏
  • 举报
回复
select * from 表 a where not exists(select * from 表 where id=a.id and flag<a.flag)
duan17 2005-02-18
  • 打赏
  • 举报
回复
flag 不用管
jinjazz 2005-02-18
  • 打赏
  • 举报
回复
flag怎么办?

34,590

社区成员

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

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