Although it does require an additional step -
select s.i
,min (s.val) over
(
partition by s.depth
,s.depth_val_seq
) as top_of_stack_val
from (select s.i
,s.val
,s.depth
,count (s.val) over
(
partition by s.depth
order by s.i
rows between unbounded preceding and current row
) as depth_val_seq
from (select s.i
,s.val
,sum (case s.op when 'I' then 1 else -1 end) over
(
order by s.i
rows between unbounded preceding and current row
) as depth
from stack_trace s
)
s
)
s
order by i
;