create table test1 (col1 number,col2 varchar2(5),col3 date,col4 number,col5 number,col6 number)
Table created.
insert into test1 values(1,'A',trunc(sysdate),0,5.5,2)
1 row(s) inserted.
insert into test1 values(1,'A',trunc(sysdate)+1,0,3.5,3)
1 row(s) inserted.
insert into test1 values(1,'A',trunc(sysdate)+2,0,2.5,0)
1 row(s) inserted.
insert into test1 values(1,'A',trunc(sysdate)+3,0,6.5,0)
1 row(s) inserted.
insert into test1 values(1,'A',trunc(sysdate)+4,0,8,0)
1 row(s) inserted.
select test1.*,sum(col5) over (partition by col1,col2 order by col3 desc,col4 desc rows col6 preceding ) derived_field from test1
where col1=1 order by col1,col2,col3,col4
COL1 | COL2 | COL3 | COL4 | COL5 | COL6 | DERIVED_FIELD | 1 | A | 12-DEC-18 | 0 | 5.5 | 2 | 11.5 | 1 | A | 13-DEC-18 | 0 | 3.5 | 3 | 20.5 | 1 | A | 14-DEC-18 | 0 | 2.5 | 0 | 2.5 | 1 | A | 15-DEC-18 | 0 | 6.5 | 0 | 6.5 | 1 | A | 16-DEC-18 | 0 | 8 | 0 | 8 |
---|
insert into test1 values(2,'B',trunc(sysdate),0,5.5,2)
1 row(s) inserted.
insert into test1 values(2,'B',trunc(sysdate),1,3.5,3)
1 row(s) inserted.
insert into test1 values(2,'B',trunc(sysdate)+1,0,3.5,5)
1 row(s) inserted.
insert into test1 values(2,'B',trunc(sysdate)+2,0,2.5,2)
1 row(s) inserted.
insert into test1 values(2,'B',trunc(sysdate)+3,0,6.5,0)
1 row(s) inserted.
insert into test1 values(2,'B',trunc(sysdate)+4,0,8,0)
1 row(s) inserted.
insert into test1 values(2,'B',trunc(sysdate)+4,1,7.9,0)
1 row(s) inserted.
insert into test1 values(2,'B',trunc(sysdate)+5,0,10.5,0)
1 row(s) inserted.
insert into test1 values(2,'B',trunc(sysdate)+6,0,1.5,0)
1 row(s) inserted.
select test1.*,sum(col5) over (partition by col1,col2 order by col3 desc,col4 desc rows col6 preceding )derived_field from test1
where col1=2 order by col1,col2,col3,col4
COL1 | COL2 | COL3 | COL4 | COL5 | COL6 | DERIVED_FIELD | 2 | B | 12-DEC-18 | 0 | 5.5 | 2 | 12.5 | 2 | B | 12-DEC-18 | 1 | 3.5 | 3 | 16 | 2 | B | 13-DEC-18 | 0 | 3.5 | 5 | 38.9 | 2 | B | 14-DEC-18 | 0 | 2.5 | 2 | 17 | 2 | B | 15-DEC-18 | 0 | 6.5 | 0 | 6.5 | 2 | B | 16-DEC-18 | 0 | 8 | 0 | 8 | 2 | B | 16-DEC-18 | 1 | 7.9 | 0 | 7.9 | 2 | B | 17-DEC-18 | 0 | 10.5 | 0 | 10.5 | 2 | B | 18-DEC-18 | 0 | 1.5 | 0 | 1.5 |
---|