有表T,其中有列 id,a,b,c, id是主键,a有重复的值,b是时间,c是一个值。现在想筛选出这样的数据:
    条件:得到不同的a值(分组a),其中要求b的值最大(having 或者max),
    值:  得到 id,a,b,c. 其中c必须有。

解决方案 »

  1.   

    试试看:
    select * from t where (a,b) in (select a,max(b) from t group by a)
      

  2.   

    提供另一种语法select a,b,c
      (
        select a,b,c
               ,row_number() over(partition by a order by b desc) rn
           from
       )
      where rn = 1
      

  3.   


    select * from T t1
    where not exists(select null from T t2 where t2.id=t1.id and t2.b>t1.b)
      

  4.   


    select m.* from t m where not exists(select 1 from t n where n.a = m.a and n.b > m.b)select m.* from t m where b = (select max(b) from t n where n.a = m.a)