假设表名字A ,有三字段id(int) 、a(字符型)、b(日期型)。
我想找出,在b同年同月中,去掉a的重复值,只保存id最大的
比如:id   a   b
      1   x   2013-04-25
      2   x   2013-04-12
      3   x   2013-03-25
      4   y   2012-04-21
要得到的值:id   a   b
           1   x   2013-04-25
           3   x   2013-03-25
           4   y   2012-04-21      

解决方案 »

  1.   

    select * from a t where exists(select 1 from a where x=t.x and id<t.id)
      

  2.   

    SQL> select * from test;        ID A  B
    ---------- -- ---------
             1 x  25-APR-13
             2 x  12-APR-13
             3 x  25-MAR-13
             4 y  21-APR-12---求最小
    SQL>  select * from test where b in (select min(b) from test group by extract(year from b) || extract(month from b));        ID A  B
    ---------- -- ---------
             2 x  12-APR-13
             3 x  25-MAR-13
             4 y  21-APR-12
    ---求最大
    SQL> select * from test where b in (select max(b) from test group by extract(year from b) || extract(month from b));        ID A  B
    ---------- -- ---------
             1 x  25-APR-13
             3 x  25-MAR-13
             4 y  21-APR-12SQL> 
      

  3.   

    上面那个有点小问题,用这个吧SQL> select * from test where b in (select min(b) from test group by extract(year from b) || extract(month from b) || a);        ID A  B
    ---------- -- ---------
             2 x  12-APR-13
             3 x  25-MAR-13
             4 y  21-APR-12
      

  4.   

    谢谢楼上的了,解决了,虽然要求跟你这样不一样,但是让我学习到一个新的oracle函数