You can use analytical function as follows:
Select product,
Sum(case when rnd=1 then price else -price end) as diff
From
(Select t.*,
Row_number() over (partition by product order by date) as rn,
Row_number() over (partition by product order by date desc) as rnd
From your_table t) t
Where 1 in (rnd, rn)
Group by product;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…