yzm698353(提问者)感谢,这样应该可以?
;with t as (
select 初始数量,料号,CONVERT(date,\'2021-1-1\') as 初始日期,0 as operate from 初始库存表A
union all
select 销售数量,料号,销售日期,-1 from 销售表B
union all
select 入库数量,料号,入库日期,1 from 入库表C
),t1 as (
select *,ROW_NUMBER() over(partition by 料号 order by 初始日期) as 排序 from t
),t2 as (
select *,初始数量 as 当前库存 from t1 where operate=0
union all
select a.*,b.当前库存+(a.operate*a.初始数量) from t1 a,t2 b where a.排序=b.排序+1 and a.料号=b.料号
)
select * from t2