3,491
社区成员
发帖
与我相关
我的任务
分享
SELECT t.*,
nvl(last_value(nullif(c, 0) ignore NULLS) over(ORDER BY b), 0) a1
FROM t;
A B C A1
---- --------------------------------------- --------------------------------------- ----------
2012 1 20 20
2012 2 0 20
2012 3 50 50
2012 4 0 50
2012 5 0 50
2012 6 0 50
2012 7 0 50
2012 8 80 80
2012 9 0 80
2012 10 0 80
2012 11 90 90
2012 12 0 90
12 rows selected
今天看到一个更简单的方法
create table T
(
A VARCHAR2(4),
B INTEGER,
C INTEGER
)
;
insert into T (A, B, C)
values ('2012', 1, 20);
insert into T (A, B, C)
values ('2012', 2, 0);
insert into T (A, B, C)
values ('2012', 3, 50);
insert into T (A, B, C)
values ('2012', 4, 0);
insert into T (A, B, C)
values ('2012', 5, 0);
insert into T (A, B, C)
values ('2012', 6, 0);
insert into T (A, B, C)
values ('2012', 7, 0);
insert into T (A, B, C)
values ('2012', 8, 80);
insert into T (A, B, C)
values ('2012', 9, 0);
insert into T (A, B, C)
values ('2012', 10, 0);
insert into T (A, B, C)
values ('2012', 11, 90);
insert into T (A, B, C)
values ('2012', 12, 0);
commit;
select t.a,t.b,t1.c
from t,
(select a, b, lead(b, 1) over(order by a, b) r, c from t where c <> 0) t1
where t.b >= t1.b
and (t.b < t1.r or t1.r is null)
A B C
---- --------------------------------------- ---------------------------------------
2012 1 20
2012 2 20
2012 3 50
2012 4 50
2012 5 50
2012 6 50
2012 7 50
2012 8 80
2012 9 80
2012 10 80
2012 11 90
2012 12 90
12 rows selected
select b.a,b.b,case when c<>0 then c else (select sum(c) from view_tst_aaa a where a.b<=b.b and a.b>=(select max(c.b) from view_tst_aaa c where c.c>0 and c.b<=b.b)) end
from view_tst_aaa b