Используйте lag()
, чтобы определить, где происходит уменьшение. Тогда накопительный для группировки. И, наконец, расположите нужные вам числа:
select t.*,
(case when prev_price < price
then row_number() over (partition by grp order by date, hour) - 1
else 0
end) as cumulative_increases,
(case when prev_price < price
then (max(price) over (partition by grp order by date, hour) * 1.0 /
min(price) over (partition by grp order by date, hour)
) - 1
else 0
end) as cumulative_increases,
from (select t.*,
sum(case when prev_price <= price then 0 else 1 end) over (order by date, hour) as grp
from (select t.*,
lag(price) over (order by date, hour) as prev_price
from t
) t
) t;
Здесь - это скрипта db <>.