如:id  time
    1   2010-10-9
    1   2010-10-20
    1   2010-11-10
    2   2010-11-11
    2   2010-11-12
    3   2010-11-18
    3   2010-10-8
取得每个Id号最后时间段的数

解决方案 »

  1.   

    select  * from tb t where time=(select max(time) from tb where id=t.id)
      

  2.   

    select id,max([time]) from tb group by id
      

  3.   

    create table #tb(id int,time datetime)
    go
    insert #tb
    select 1,'2010-10-9' union all
    select 1,'2010-10-20' union all
    select 1,'2010-11-10' union all
    select 2,'2010-11-11' union all
    select 2,'2010-11-12' union all
    select 3,'2010-11-18' union all
    select 3,'2010-10-8'
    go用max()函数select id,max(time) from #tb group by id用子查询select  * from #tb t where time=(select max(time) from #tb where id=t.id)
      

  4.   

    select  * from #tb t where not exists(select time from #tb where t.time<#tb.time)