oracle 中 GROUP BY 必须跟select中用到的所有字段?

解决方案 »

  1.   

    select * from (select t.*,row_number() over(partition by id order by history desc) rn from tablename) where rn = 1
      

  2.   

    select a.* from tablename a,(select id,max(history) from tablename group by id) b
    where a.id = b.id
    and a.history = b.history;
      

  3.   

    漏了别名:
    select * from (select t.*,row_number() over(partition by id order by history desc) rn from tablename t) where rn = 1
      

  4.   

    select * from test s where rowid in (select max(rowid) from test group by id)
      

  5.   

    select * from tablename where rowid in (select max(rowid) from test group by id)
      

  6.   

    呵呵。。还是把表名写错了
    select * from tablename s where rowid in (select max(rowid) from tablename group by id)
      

  7.   

    如果要取第一到第二条:
    select a.* from tablename a,(select id,max(history)as history2 from tablename group by id) b
    where a.id = b.id
    and a.history = b.history2
    and rownum <= 2
      

  8.   

    select * from tablename where (id,history) in (select id,max(history) history from tablename group by id) order by id;
      

  9.   

    select id,max(name),max(history)
    from tablename
    group by id