| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
|  |  | |||
Home -> Community -> Usenet -> c.d.o.server -> Re: rownum & group by question
Something like this:
ORA92> select * from tpivot;
    CLT_ID      TX_YE        AMT
---------- ---------- ----------
         1       2006         10
         1       2006         20
         1       2005          8
         1       2004         20
         1       2003        400
         2       2006        100
         2       2005        200
         2       2005        100
         2       2004          8
         2       2003          7
10 rows selected.
ORA92>
ORA92> select rownum, t.*
  2  from (select CLT_ID, TX_YE, sum(AMT) sum_amt
3 from tpivot 4 where clt_id = 1 5 group by CLT_ID, TX_YE 6 order by clt_id, tx_ye desc) t7 where rownum < 3
    ROWNUM     CLT_ID      TX_YE    SUM_AMT
---------- ---------- ---------- ----------
         1          1       2006         30
         2          1       2005          8
ORA92>
ORA92> select t.clt_id, max(tx_ye) max_tx_ye,
2 sum(decode(rownum, 1, t.sum_amt)) y1, 3 sum(decode(rownum, 2, t.sum_amt)) y2 4 from (select CLT_ID, TX_YE, sum(AMT) sum_amt 5 from tpivot 6 where clt_id = 1 7 group by CLT_ID, TX_YE 8 order by clt_id, tx_ye desc) t9 where rownum < 3
    CLT_ID  MAX_TX_YE         Y1         Y2
---------- ---------- ---------- ----------
1 2006 30 8 Received on Wed Mar 22 2006 - 11:21:38 CST
|  |  |