用分析函数。。lag就是上一条记录,lead是下一条记录...
select
tmp.ORDER_DATE,
tmp.PRODUCT_ID,
tmp.QTY,
tmp.上一天日期,
t2.qty as 上一天数量,
tmp.qty-nvl(t2.qty,0) as 相减的数量
from (
select
t1.*,
lag(t1.order_date,1) over (order by order_date) as 上一天日期
from (
select to_date('25-09-2021','dd-mm-yyyy') order_date,1000 product_id,100 qty from dual union all
select to_date('26-09-2021','dd-mm-yyyy'),2000,150 from dual union all
select to_date('27-09-2021','dd-mm-yyyy'),1000,160 from dual union all
select to_date('28-09-2021','dd-mm-yyyy'),2000,120 from dual union all
select to_date('29-09-2021','dd-mm-yyyy'),2000,200 from dual union all
select to_date('30-09-2021','dd-mm-yyyy'),1000,90 from dual) t1) tmp
left join (
select to_date('25-09-2021','dd-mm-yyyy') order_date,1000 product_id,100 qty from dual union all
select to_date('26-09-2021','dd-mm-yyyy'),2000,150 from dual union all
select to_date('27-09-2021','dd-mm-yyyy'),1000,160 from dual union all
select to_date('28-09-2021','dd-mm-yyyy'),2000,120 from dual union all
select to_date('29-09-2021','dd-mm-yyyy'),2000,200 from dual union all
select to_date('30-09-2021','dd-mm-yyyy'),1000,90 from dual) t2 on tmp.上一天日期=t2.order_date
order by tmp.order_date