现有表如下 :
 id   time
 1     10
 2      10
3       11
4       11
5       13
6       16 
要求写查询语句 求出 
2  10
4  11
5  13
6  16
既求出不同的套 time行 ,如果time 行相同 取id 大值 

解决方案 »

  1.   

    select * from table where id in (select max(id) from table group by time);
      

  2.   

    SELECT
        MAX(ID) AS ID,
        time
    FROM tb
    GROUP BY time
      

  3.   

    select * from tb T1 where not exists (select 1 from tb T2 T1.time=T2.time and T1.id<T2.id)
      

  4.   

    select id,time from tab where id<>'1' and id<>'3'
      

  5.   


    select * from TB where id in (select max(id) from TB group by [time])
    ---- 或者 ------
    select max(id) AS ID, [time] from TB group by [time] order by id
      

  6.   


    declare @tb table([id] int,[time] int)
    insert @tb
    select 1,10 union all
    select 2,10 union all
    select 3,11 union all
    select 4,11 union all
    select 5,13 union all
    select 6,16select * from @tb t
    where not exists(select 1 from @tb where [id]>t.[id] and time=t.[time])
    --测试结果:
    /*
    id  time
    2   10
    4   11
    5   13
    6   16
    */
      

  7.   

    --设表名为t1
    select * from t1 as a 
    where not exists(select * from t1 where time=a.time and id>a.id)