表test 数据如下:
id   num  date
1    35   2010-9-10 
1    36   2010-9-11 
1    34   2010-9-12 
2    35   2010-9-10 
2    34   2010-9-11 
3    34   2010-9-12 
3    35   2010-9-11 
3    36   2010-9-10 查询结果如下:
id   num  date
1    34   2010-9-12 
2    34   2010-9-11 
3    34   2010-9-12 要求:按id去掉重复记录,按date排序 取date最大的那一条请大侠们指教

解决方案 »

  1.   

    select id, num ,date from TB a where not exists (select 1 from TB where a.id=id And a.date<date )
      

  2.   

    首先过滤掉id重复的记录 即id重复的只保留一条 那么保留那一条呢?就是日期最大的那条 就这样怎么写呢????
      

  3.   

    create table #(id int,num int,date varchar(10))
    insert into #select 1 ,35, '2010-9-10'union all 
    select 1 ,36, '2010-9-11'union all 
    select 1 ,34 ,'2010-9-12'union all 
    select 2 ,35 ,'2010-9-10'union all 
    select 2 ,34 ,'2010-9-11'union all 
    select 3 ,34 ,'2010-9-12'union all 
    select 3 ,35 ,'2010-9-11'union all 
    select 3 ,36 ,'2010-9-10'  select id, num ,date from # a where not exists (select 1 from # b where a.id=b.id And a.date<b.date )
    1 34 2010-9-12
    2 34 2010-9-11
    3 34 2010-9-12