|
|
select mtime,incomt,outgo
select mtime,income,outgo,
sum(income-outgo)over(order by mtime
rows between unbounded preceding and current row ) rest,
row_number()over(partition by mtime order by mtime ) rn
from item_a;
-----具体过程如下-----------------------------------------------------
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
会话已更改。
SQL> create table item_a
2 (mtime date,
3 income number,
4 outgo number
5 );
表已创建。
SQL> insert into item_a values(to_date('20050301','yyyymmdd'),50,30);
已创建 1 行。
SQL> insert into item_a values(to_date('20050302','yyyymmdd'),45,60);
已创建 1 行。
SQL> insert into item_a values(to_date('20050305','yyyymmdd'),60,10);
已创建 1 行。
SQL> commit;
提交完成。
SQL> select mtime,income,outgo,rest from
2 (select mtime,income,outgo,
3 sum(income-outgo)over(order by mtime) rest,
4 row_number()over(partition by mtime order by mtime desc)rn
5 from item_a
6 )where rn=1;
MTIME INCOME OUTGO REST
------------------- ---------- ---------- ----------
2005-03-01 00:00:00 50 30 20
2005-03-02 00:00:00 45 60 5
2005-03-05 00:00:00 60 10 55
SQL> insert into item_a values(to_date('20050305','yyyymmdd'),60,10);
已创建 1 行。
SQL> select mtime,income,outgo,rest from
2 (select mtime,income,outgo,
3 sum(income-outgo)over(order by mtime) rest,
4 row_number()over(partition by mtime order by mtime desc)rn
5 from item_a
6 )where rn=1;
MTIME INCOME OUTGO REST
------------------- ---------- ---------- ----------
2005-03-01 00:00:00 50 30 20
2005-03-02 00:00:00 45 60 5
2005-03-05 00:00:00 60 10 105
SQL> insert into item_a values(to_date('20050303','yyyymmdd'),0,0);
已创建 1 行。
SQL> insert into item_a values(to_date('20050304','yyyymmdd'),0,0);
已创建 1 行。
SQL> commit;
提交完成。
SQL> select mtime,income,outgo,rest from
2 (select mtime,income,outgo,
3 sum(income-outgo)over(order by mtime) rest,
4 row_number()over(partition by mtime order by mtime desc)rn
5 from item_a
6 )where rn=1;
MTIME INCOME OUTGO REST
------------------- ---------- ---------- ----------
2005-03-01 00:00:00 50 30 20
2005-03-02 00:00:00 45 60 5
2005-03-03 00:00:00 0 0 5
2005-03-04 00:00:00 0 0 5
2005-03-05 00:00:00 60 10 105
SQL> |
|