add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
create table t as
select v.*
from (values (1, 10, 20, 100, 30),
(2, 20, 40, 200, 60),
(3, 30, 60, 0 , 260),
(4, 40, 70, 0 , 200),
(5, 50, 80, 50 , 130)
) v(Row, Col1, Col2, Col3, Result)

5 rows affected
select t.*,
(case when col3 <> 0 then col1 + col2
else sum(col2 + col3) over (partition by grp order by row desc)
end) as result
from (select t.*,
sum(case when col3 <> 0 then 1 else 0 end) over (order by row desc) as grp
from t
) t
order by row
row col1 col2 col3 result grp result
1 10 20 100 30 3 30
2 20 40 200 60 2 60
3 30 60 0 260 1 260
4 40 70 0 200 1 200
5 50 80 50 130 1 130