一个表有3个字段 ID,Time,RYID 
ID字段是外键对应另一个表的ID,可重复
Time字段是时间
RYID字段是我想要的字段
我需要查询出 在ID字段相同的情况下,Time字段最大的那条数据的RYID字段的值.
现在写出的SQL是这样..出不来RYID字段
select ID,max(Time) from T_Test Group by ID我应该怎么取RYID呢?这样也不行,因为不同的ID 可能有相同的Time
select * from T_Test where Time in (select max(Time)  from T_Test Group by ID)

解决方案 »

  1.   


    select * from T_Test t where [Time]=
    (select max([Time]) from T_Test where ID=t.ID) order by ID
      

  2.   


    select * from tb a 
    where not exists(select 1 from tb where id=a.id and a.[time]<[time])
      

  3.   

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

  4.   


    select * from 
    (select row_number() over(partiton by id order by [time] desc) as no,* from tb) a
    where no=1