select * from 职工表 tem where 加入日期=(select max(加入日期) from 职工表  where 单位编号=tem.单位编号)

解决方案 »

  1.   

    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.单位编号=单位编号 )
      

  2.   

    --查每个单位中最新加入前3的职工
    select * from 职工表 a where 加入日期 in (select top 3 加入日期 from 职工表  where 单位编号=a.单位编号 order by 加入日期 desc)
      

  3.   

    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)