如何取分组后的第一条记录

littlerainsnow 2006-12-01 10:10:54
insert into #t select '123456','邯郸','网络部','编辑'
union all select '544544','邯郸','网络部','技术'
union all select '344544','邯郸','网络部','技术'
union all select '144576','邯郸','网络部','主任'
union all select '144574','邯郸','网络部','主编'
union all select '124574','邯郸','网络部','编辑'


得到

teachid school workbumen workpost
123456 邯郸 网络部 编辑
544544 邯郸 网络部 技术
144576 邯郸 网络部 主任
144574 邯郸 网络部 主编


谢谢了
...全文
170 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
userqin 2006-12-01
  • 打赏
  • 举报
回复
select * from
( select * ,isnull((select count(*) from #t where school=a.school and workbumen=a.workbumen and workpost=a.workpost and teachid<=a.teachid),0) as count from #t a group by school,teachid,workbumen,workpost) b where b.count<=1
dawugui 2006-12-01
  • 打赏
  • 举报
回复
select * from TB b
where workpost=(select top 1 workpost from TB where teachid = b.teachid)
冷箫轻笛 2006-12-01
  • 打赏
  • 举报
回复
select identity(int,1,1) as id ,*
insert into #tt
from t

select * from #tt a
where not exists(select 1 from #tt where school = a.school and workbumen = a.workbumen and id < a.id)

drop table #tt
dawugui 2006-12-01
  • 打赏
  • 举报
回复
如何将表中的相同ID号的第一条记录取出来

表:a
id name
11 aaaa
11 bbbb
11 cccc
22 dddd
22 eeee
22 ffff

如何将表中的相同id号的第一条记录取出来?即:
id name
11 aaaa
22 dddd


CREATE TABLE #a (
[id] [char] (10),
[name] [char] (10)
)

insert into #a(id,name) values('11','aaaa')
insert into #a(id,name) values('11','bbbb')
insert into #a(id,name) values('11','cccc')
insert into #a(id,name) values('22','dddd')
insert into #a(id,name) values('22','eeee')
insert into #a(id,name) values('22','ffff')

select * from #a b
where name=(select top 1 name from #a where id=b.id)

drop table #a

id name
---------- ----------
11 aaaa
22 dddd

(所影响的行数为 2 行)




CREATE TABLE #a (
[id] [char] (10),
[name] [char] (10)
)

insert into #a(id,name) values('11','aaaa')
insert into #a(id,name) values('11','bbbb')
insert into #a(id,name) values('11','cccc')
insert into #a(id,name) values('22','dddd')
insert into #a(id,name) values('22','eeee')
insert into #a(id,name) values('22','ffff')

select id1=identity(int,1,1),* into #t from #a
go
select id,name from #t where id1 in(select min(id1) from #t group by id)

drop table #a
drop table #t

id name
---------- ----------
11 aaaa
22 dddd

(所影响的行数为 2 行)

34,590

社区成员

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

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