一个滚动算累计值得难题,谢谢
ID je(金额) rs(人数)
1 10 2
2 20 5
1 5 1
1 40 4
1 100 10
希望得到如下结果
即当type=2的时候 累计je=上次的je(10)+本次rs(5)*上次的 je(10)/rs(2)得5
也就是 10+5*(10/2) = 35
ID je rs
1 10 2
2 35 7
1 40 8
1 80 12
1 180 22
CREATE TABLE TEST
(ID NUMBER,
JE NUMBER,
RS NUMBER);
INSERT INTO TEST VALUES(1,10,2);
INSERT INTO TEST VALUES(2,20,5);
INSERT INTO TEST VALUES(1,5,1);
INSERT INTO TEST VALUES(1,40,4);
INSERT INTO TEST VALUES(1,100,10);
COMMIT;
1 10 2
2 20 5
1 5 1
1 40 4
1 100 10
希望得到如下结果
即当type=2的时候 累计je=上次的je(10)+本次rs(5)*上次的 je(10)/rs(2)得5
也就是 10+5*(10/2) = 35
ID je rs
1 10 2
2 35 7
1 40 8
1 80 12
1 180 22
CREATE TABLE TEST
(ID NUMBER,
JE NUMBER,
RS NUMBER);
INSERT INTO TEST VALUES(1,10,2);
INSERT INTO TEST VALUES(2,20,5);
INSERT INTO TEST VALUES(1,5,1);
INSERT INTO TEST VALUES(1,40,4);
INSERT INTO TEST VALUES(1,100,10);
COMMIT;
select id,rownum rnum,sum(je)over(order by rownum) je,sum(rs)over(order by rownum) rs from (
select a.id,rownum,decode(a.id,2,a.rs*lag(je)over(order by rownum)/lag(rs)over(order by rownum),je) je,rs from test a)a;
id rownum je rs
1 1 10 2
2 2 25 5
1 3 5 1
1 4 40 4
1 5 100 10
2 6 220 22
逐條比較大小
select a.id,rownum,je+lag(jnr)over(order by rownum) from (
select a.id,rownum,sum(-je)over(partition by rownum order by rownum) jnr,je from test a )a