去重的问题

wth1150 2006-09-05 09:27:40
如表:
q1 q2 q3 q4
1 w e s
2 w d f
5 e e f
3 d d s
4 r h j
1 g e s
5 f d d
7 l p j
结果要:
q1 q2 q3 q4
1 w e s
2 w d f
3 d d s
4 r h j
5 e e f
7 l p j
...全文
227 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzq111 2006-09-06
  • 打赏
  • 举报
回复
题目没有说清楚,其实结果可能不唯一的.id为5的到底去哪条,随机删除一条,留一条?
wth1150 2006-09-06
  • 打赏
  • 举报
回复
select * from del_person_temp a
where id=(select top 1 id from del_person_temp where comp_nme=a.comp_nme order by id)


select * from del_person_temp where id in(select min(id) from del_person_temp group by comp_nme)
select min(id) from del_person_temp group by comp_nme
jaway 2006-09-05
  • 打赏
  • 举报
回复
CREATE TABLE # (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[q1] [int] NULL ,
[q2] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[q3] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[q4] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
select * from #
insert # (q1,q2,q3,q4)
select * from a

select #.q1,#.q2,#.q3,#.q4 from # join(
select min(id) as id,q1 from # group by q1) b on #.id=b.id
drop table #
yuehaiyang 2006-09-05
  • 打赏
  • 举报
回复
楼上算一种
还有其它办法,用having
hellowork 2006-09-05
  • 打赏
  • 举报
回复
select id = identity(int,1,1),* into #tmp from table

select q1,q2,q3,q4 from #tmp a where
not exists(select 1 from #tmp where q1 = a.q1 and id < a.id)

27,579

社区成员

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

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