表T
id(为数值类型,以1递增插入作为主键的)
name
orderId假设有如下这些记录:
1,A,101
2,B,101
3,C,101
4,A,102
5,B,102
6,A,103
7,B,103
8,C,103
9,D,103现在我要查出这个表里orderId相关的最后一条记录,执行查询语句,因得到如下结果:
3,C,101
5,B,102
9,D,103要求,每个orderId显示一条记录,且这条记录是显示id最大的那条请问这条语句怎么写????????(ORACLE数据库)

解决方案 »

  1.   

    select * from table where id in(select max(id) from tablename group by orderid)
      

  2.   

    已解决,实际SQL如下
    select ha2.hproci_,ha2.activity_name_  from JBPM4_HIST_ACTINST ha2 where dbid_ in( select max(ha.dbid_) from JBPM4_HIST_ACTINST ha where ha.activity_name_!='L' group by ha.hproci_ ) and ha2.hproci_='2660211'
      

  3.   

    delete from t where rowid not in (select rowid from (
    select max(T1) from t group by T3))
      

  4.   

    select id,name,orderId
    from(
    select id,name,orderId,row_number()over(partition by orderId order by name desc) rn
    from t)
    where rn=1
      

  5.   

    with t as 
    (select 1 id,'A' name,101 orderid from dual
    union all
    select 2 id,'B' name ,101 orderid from dual
    union all
    select 3 id,'C' name,101 orderid from dual
    union all
    select 4 id,'A' name,102 orderid from dual
    union all
    select 5 id,'B' name,102 orderid from dual
    union all
    select 6 id,'A' name,103 orderid from dual
    union all
    select 7 id,'B' name,103 orderid from dual
    union all
    select 8 id,'C' name,103 orderid from dual
    union all
    select 9 id,'D' name,103 orderid from dual)
    select min(t.rowid) from t;
    create table t as select 1 id,'A' name,101 orderid from dual
    union all
    select 2 id,'B' name ,101 orderid from dual
    union all
    select 3 id,'C' name,101 orderid from dual
    union all
    select 4 id,'A' name,102 orderid from dual
    union all
    select 5 id,'B' name,102 orderid from dual
    union all
    select 6 id,'A' name,103 orderid from dual
    union all
    select 7 id,'B' name,103 orderid from dual
    union all
    select 8 id,'C' name,103 orderid from dual
    union all
    select 9 id,'D' name,103 orderid from dual;select *
      from (select t.*, last_value(id) over(partition by orderid) rn
              from t
             order by id) a
     where a.rn = a.id;