比如有个表table
id     col      datetime
1      aa      2012-01-01
2      aa      2012-01-03
3      bb      2012-03-01
4      cc      2011-10-12
5      bb      2013-11-11我要得到
2      aa      2012-01-03
5      bb      2013-11-11
4      cc      2011-10-12
就是col字段相同的,只取datetime最后的那一条记录~~拜托了~~

解决方案 »

  1.   


    select *
    from tb t
    where not exists (select 1 from tb where col=t.col and date>t.date)
      

  2.   


    select *
    from tb t
    where id = (select top 1 id from tb where col=t.col order by date desc)select *
    from tb t
    where date = (select max(date) from tb where col=t.col)select *
    from(
        select *,rid=row_number() over (partition by col order by date desc)
        from tb
    )t
    where rid = 1-- ...
      

  3.   

    select id,col,max(datetime)
    from tb 
    group by id,col