怎样用select语句查询

dtljf 2003-09-01 10:46:37
下图一个存放多个单位职工的表,如何查询每个单位中最新加入的职工?
单位编号 职工编号 加入日期
001 1 2002-05-02
001 2 2003-04-01
002 6 2001-02-15
002 5 2000-12-03
003 3 2003-04-02

请高手指教,谢谢!!!
...全文
40 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
愉快的登山者 2003-09-01
  • 打赏
  • 举报
回复
select * from table1 A where 加入日期 = (select max(加入日期) from table1 where 单位编号 = A.单位编号)

前2个:
select * from table1 A where 加入日期 in (select top 2 加入日期 from table1 where 单位编号 = A.单位编号 order by 加入日期 desc)
sdhdy 2003-09-01
  • 打赏
  • 举报
回复
--查每个单位中最新加入前3的职工
select * from 职工表 a where 加入日期 in (select top 3 加入日期 from 职工表 where 单位编号=a.单位编号 order by 加入日期 desc)
dafu71 2003-09-01
  • 打赏
  • 举报
回复
declare @test table (单位编号 varChar(3),职工编号 VarChar(2),加入日期 datetime)
INSERT INTO @TEST(单位编号,职工编号,加入日期) VALUES('001','1','2002-05-02')
INSERT INTO @TEST(单位编号,职工编号,加入日期) VALUES('001','2','2003-04-01')
INSERT INTO @TEST(单位编号,职工编号,加入日期) VALUES('002','6','2001-02-15')
INSERT INTO @TEST(单位编号,职工编号,加入日期) VALUES('002','5','2000-12-03')
INSERT INTO @TEST(单位编号,职工编号,加入日期) VALUES('003','3','2003-04-02')
select * from @TEST tem where 加入日期=(select max(加入日期) from @TEST where 单位编号=tem.单位编号)
or
select * from @test x where 加入日期 in (select max(加入日期) from @test where x.单位编号=单位编号 )
flying120 2003-09-01
  • 打赏
  • 举报
回复
up ^_^
pengdali 2003-09-01
  • 打赏
  • 举报
回复
select * from 职工表 tem where 加入日期=(select max(加入日期) from 职工表 where 单位编号=tem.单位编号)

34,588

社区成员

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

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