без аналитики
select sum(a.num),a.str, nvl(b.str,c.str)
from
(select id,str,num, rownum rn from tab1) a,
(select id,str,num, rownum rn from tab1) b,
(select id,str,num, rownum rn from tab1) c
where b.rn(+)=a.rn+1
and b.str(+)=a.str
and c.rn(+)=a.rn-1
and c.str(+)=a.str
group by a.str, nvl(b.str,c.str)
с аналитикой
SELECT DISTINCT str,
SUM (num) OVER (PARTITION BY str, decode (str,
lagstr, 1,
leadstr, 1,
0
))
FROM (SELECT ID, str, num, LAG (str, 1) OVER (ORDER BY ID) lagstr,
LEAD (str, 1) OVER (ORDER BY ID) leadstr
FROM tab1)
но чет в первом случае таблицу на три раза, а во втором дистинкт
наверно как то лучше можно