兄弟们问个 sql 合并相同ID 取最后时间表 A
ID    TIME
1     2010-2-6 9:58:52
1     2011-3-8 10:08:52
2     2000-2-6 9:58:52
2     2001-3-11 11:08:52
3     2009-5-22 13:08:52
查询得到 
ID    TIME
1     2011-3-8 10:08:52
2     2001-3-11 11:08:52
3     2009-5-22 13:08:52

解决方案 »

  1.   

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

  2.   

    select ID,MAX([TIME]) as [time]
    from a 
    group by ID
      

  3.   

    if object_id('[tA]') is not null drop table [tA]
    go
    create table [tA] (ID int,TIME datetime)
    insert into [tA]
    select 1,'2010-2-6 9:58:52' union all
    select 1,'2011-3-8 10:08:52' union all
    select 2,'2000-2-6 9:58:52' union all
    select 2,'2001-3-11 11:08:52' union all
    select 3,'2009-5-22 13:08:52'
    select * 
    from ta t
    where time=(select max(time) from ta where id=t.id )
    order by id
    /*
    ID          TIME
    ----------- -----------------------
    1           2011-03-08 10:08:52.000
    2           2001-03-11 11:08:52.000
    3           2009-05-22 13:08:52.000(3 個資料列受到影響)
    */
      

  4.   

    select id , max(time) time from a group by id
      

  5.   

    select id,max(TIME) as TIME
    from tb
      

  6.   

    select id , max(time) time from a group by id
      

  7.   

    select ID,MAX([TIME]) as [time]
    from A 
    group by ID