怎样才能算出来第一张表中的【订单数量】在第二张表哪些数量才能满足,例如:表1中的第一行,需求数量10,那么在第二个表中需要第一行,二行才能满足,那么把和表2中的采购订单号、要求交货日期更新到表1中去。
6条回答 默认 最新
- CSDN专家-文盲老顾 2021-06-04 11:01关注
with t1(序号,订单号,要求日期,物料编码,订单数量) as ( select 1,'so21001',CONVERT(date,'2021-6-10'),1001,10 union all select 2,'so21002','2021-6-11',1001,20 union all select 3,'so21003','2021-6-12',1001,10 union all select 4,'so21004','2021-6-13',1001,50 union all select 5,'so21005','2021-6-14',1001,60 union all select 6,'so21006','2021-6-15',1001,100 union all select 7,'so21007','2021-6-16',1001,30 union all select 8,'so21008','2021-6-17',1001,10 union all select 9,'so21009','2021-6-18',1001,20 union all select 10,'so21010','2021-6-19',1001,10 ),t2(序号,采购订单号,要求交货日期,物料编码,订单数量) as ( select 1,'po21001',CONVERT(date,'2021-6-2'),1001,5 union all select 2,'po2','2021-6-3',1001,10 union all select 3,'po3','2021-6-4',1001,30 union all select 4,'po4','2021-6-5',1001,100 union all select 5,'po5','2021-6-5',1001,60 union all select 6,'po6','2021-6-7',1001,20 ),t3 as ( select *,ROW_NUMBER() over(partition by 物料编码 order by 要求日期,序号) as rid from t1 ),表1 as ( select * from t3 a cross apply ( select SUM(订单数量) as 累计需求数量 from t3 where 物料编码=a.物料编码 and rid<=a.rid ) b ),t5 as ( select *,ROW_NUMBER() over(partition by 物料编码 order by 要求交货日期,序号) as rid from t2 ),表2 as ( select * from t5 a cross apply ( select SUM(订单数量) as 累计在途数量 from t5 where 物料编码=a.物料编码 and rid<=a.rid ) b ) -- 可以忽视前边的内容 select a.*, 采购订单号,要求交货日期 from 表1 a outer apply ( select top 1 * from 表2 where 物料编码=a.物料编码 and 累计在途数量>=a.累计需求数量 order by 累计在途数量 ) b
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
- ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
- ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
- ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
- ¥15 运动想象脑电信号数据集.vhdr
- ¥15 三因素重复测量数据R语句编写,不存在交互作用
- ¥15 微信会员卡等级和折扣规则
- ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
- ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
- ¥15 gdf格式的脑电数据如何处理matlab