Di SQL Server 2008, Anda sangat terbatas karena Anda tidak dapat menggunakan fungsi analitik. Berikut ini tidak efisien, tetapi ini akan menyelesaikan masalah Anda:
with tg as (
select t.*, g.grp
from t cross apply
(select count(*) as grp
from t t2
where t2.pk <= t.pk and t2.pk = 0
) g
)
select tg.*, p.running_price
from tg cross apply
(select sum(tg2.price) as running_price
from tg tg2
where tg2.grp = tg.grp and tg2.pk <= tg.pk
) p;
Sayangnya, sebelum SQL Server 2012, solusi paling efisien mungkin melibatkan kursor. Di SQL Server 2012+, Anda cukup melakukan:
select t.*,
sum(price) over (partition by grp order by pk) as running_price
from (select t.*,
sum(case when price = 0 then 1 else 0 end) over (order by pk) as grp
from t
) t;