select a.MONEY_GETID,a.UNITID from test112 a,(select rownum rn,rowid ri from test112 where unitid=1 order by sf_begin_date desc) b ,(select count(*) cn from test112 where unitid=1 order by sf_begin_date desc) c
where a.rowid=b.ri and (b.rn=1 or b.rn=c.cn-1)
where a.rowid=b.ri and (b.rn=1 or b.rn=c.cn-1)
where unitid=单位 order by sf_begin_date desc and rownum<=2
最新一次:
select money_getid from test
where sf_ed_date = (select max(sf_end_date) from test);
倒数第二次:
select a.money_getid
from (selec money_getid from test order by sf_end_date desc) a
where rownum <= 2
minus
select money_getid from test
where sf_ed_date = (select max(sf_end_date) from test);
单位条件自己加吧。
----------- ---------- ---------- ----------
1500 a 24-8月 -02 24-8月 -02
1800 a 25-8月 -02 25-8月 -02
1600 a 26-8月 -02 26-8月 -02
1900 a 27-8月 -02 27-8月 -02
2500 a 28-8月 -02 28-8月 -02
3500 a 29-8月 -02 29-8月 -02
2200 a 30-8月 -02 30-8月 -02
2000 a 31-8月 -02 31-8月 -02
2100 a 01-9月 -02 01-9月 -02
1800 b 25-8月 -02 25-8月 -02
1600 b 26-8月 -02 26-8月 -02MONEY_GETID UNITID SF_BEGIN_D SF_END_DAT
----------- ---------- ---------- ----------
1900 b 27-8月 -02 27-8月 -02
2500 b 28-8月 -02 28-8月 -02
3500 b 29-8月 -02 29-8月 -02
2200 b 30-8月 -02 30-8月 -02
2000 b 31-8月 -02 31-8月 -02
2100 b 01-9月 -02 01-9月 -02
1800 c 25-8月 -02 25-8月 -02
1600 c 26-8月 -02 26-8月 -02
1900 c 27-8月 -02 27-8月 -02
2500 c 28-8月 -02 28-8月 -02
3500 c 29-8月 -02 29-8月 -02MONEY_GETID UNITID SF_BEGIN_D SF_END_DAT
----------- ---------- ---------- ----------
2200 c 30-8月 -02 30-8月 -02
2000 c 31-8月 -02 31-8月 -02
2100 c 01-9月 -02 01-9月 -02已选择25行。SQL> select a.MONEY_GETID,a.UNITID from test2 a,(select rownum rn,rowid ri from test2 where unitid='a' order by sf_begin_date desc) b ,(select count(*) cn from test2 where unitid='a' order by sf_begin_date desc) c
where a.rowid=b.ri and (b.rn=1 or b.rn=c.cn-1)
SQL> /MONEY_GETID UNITID
----------- ----------
2000 a
1500 a