Вы можете использовать lag()
функцию с агрегацией и abs()
в основном запросе как
with Components(Person_ID, Date_from, CompType, Value) as
(
select '000001',date'2003-01-01','A1',100 from dual union all
select '000001',date'2003-01-01','B1',200 from dual union all
select '000001',date'2003-01-01','C1',150 from dual union all
select '000001',date'2003-01-01','D1',180 from dual union all
select '000001',date'2003-01-01','E1',185 from dual union all
select '000001',date'2002-01-01','A1',125 from dual union all
select '000001',date'2002-01-01','B1',20 from dual union all
select '000001',date'2002-01-01','C1',130 from dual union all
select '000001',date'2002-01-01','D1',160 from dual union all
select '000001',date'2002-01-01','E1',105 from dual union all
select '000001',date'2001-01-01','A1',90 from dual union all
select '000001',date'2001-01-01','B1',200 from dual union all
select '000001',date'2001-01-01','C1',250 from dual union all
select '000001',date'2001-01-01','D1',160 from dual union all
select '000001',date'2001-01-01','E1',185 from dual
), t2 as
(
select Person_ID, Date_from, sum(Value) as sum1,
lag(sum(Value),1,0) over (order by Date_from) as sum2,
lag(sum(Value),2,0) over (order by Date_from) as sum3
from Components
where CompType != 'E1'
group by Person_ID, date_from
)
select Person_ID, date_from,
abs(sum1-sum2) as "Current Difference",
abs(sum2-sum3) as "Previous Difference"
from t2
where sum1 * sum2 * sum3 > 0;
PERSON_ID DATE_FROM Current Difference Previous Difference
000001 01.01.2003 195 265
Демо