有一个员工档案表,记录了员工各个年度考核情况,一个员工对应多条记录,m_date为该时间
字段如下:
user_name,sex,ka,kb,kc,kd,ke,kf,kg,m_date我想用一条语句选出所有员工的最新考核情况,即新记录中每个员工只有一条记录,该记录为该员工的最新时间的记录.请问怎么实现

解决方案 »

  1.   

    select t.* from tb t where m_date = (select max(m_date) from tb where user_name = t.user_name) order by t.user_nameselect t.* from tb t where not exists (select 1 from tb where user_name = t.user_name and m_date > t.m_date) order by t.user_name
      

  2.   


    create table kh(uname varchar(20),ka varchar(20),m_date datetime)
    go
    insert into kh
    select 'aaa','111','2009-9-9' union all
    select 'aaa','222','2009-8-4' union all
    select 'aaa','333','2009-10-2' union all
    select 'bbb','444','2009-4-9' union all
    select 'bbb','555','2009-9-1' union all
    select 'bbb','666','2009-7-5'select * from kh where m_date in (select max(m_date) from kh group by uname)
    aaa 333 2009-10-02 00:00:00.000
    bbb 555 2009-09-01 00:00:00.000
      

  3.   

    select t.* from tb t where m_date = (select max(m_date) from tb where user_name = t.user_name) order by t.user_name