问题描述:有表test,有4个字段a,b,c,d,其中a,b,c是varchar2类型,d是date类型。
表中数据如下:
a  b  c  d
1  1  2  01142009
1  1  3  01212009
2  1  4  01212009
2  1  5  01222009需要得到的结果如下:a  b  c  d
1  1  3  01212009
2  1  5  01222009即根据a,b分组,取出d最大的那条记录。不用row_number() over()等函数(因某些原因不支持),尽量不用子查询,
请给出一个查询效率高的方案。目前使用的是先group by a,b,c,再查出按a,b分组中的最大d,效率很差

解决方案 »

  1.   

    select * from test taba
    where not exists (select 1 from test where a=taba.a and b=taba.b and d>taba.d)
      

  2.   

    select a.* 
    from 
    test a,
    (select a,b,max(c) c from test group by a,b) a1
    where a.a = a1.a
      and a.b = a1.b
      and a.c = a1.c
      

  3.   

    UP,除了把C换成D。好象也没有别的法子勒
      

  4.   


    select * from test 
    where not exists(select 1 from test a where a=a.a and b=a.b and d<a.d)