我有一个像这样的Cycles表:
Value Name Month
10 P00006 201412
20 P00006 201501
30 P00006 201502
100 P00007 201509
200 P00007 201510
300 P00007 201511
我想基于此表创建一个视图,其中对于每个名称,我将前几个月的值相加。 这是预期的结果:
AccruedValue Name Month
10 P00006 201412
30 P00006 201501
60 P00006 201502
100 P00007 201509
300 P00007 201510
600 P00007 201511
我不确定如果不使用游标就可以实现这一点,所以任何帮助都将受到赞赏。
答案 0 :(得分:2)
测试数据:
create table cycles
(value NUMBER, name VARCHAR2(250), month VARCHAR2(250));
/
INSERT INTO cycles VALUES(10 , 'P00006','201412');
INSERT INTO cycles VALUES(20 , 'P00006','201501');
INSERT INTO cycles VALUES(30 , 'P00006','201502');
INSERT INTO cycles VALUES(100 , 'P00007','201509');
INSERT INTO cycles VALUES(200 , 'P00007','201510');
INSERT INTO cycles VALUES(300 , 'P00007','201511');
查看my_view:
create or replace view my_view as
select
sum(value) over (partition by name order by month ) as AccruedValue ,
name ,
month
from cycles;
结果:
select * from v;
10 P00006 201412
30 P00006 201501
60 P00006 201502
100 P00007 201509
300 P00007 201510
600 P00007 201511