目前有 BOM 和领料,需要根据 BOM 的需求数量,根据先进先出的原则,BOM 用了哪些领料,也就是列出那些领料对应哪条 BOM ,以及用掉多少,目前能想到的方案是用游标逐行判单,记录核销数量,但在数据量比较大的情况下,执行效率十分低,是否有比较高效的解决方案。
IF OBJECT_ID('tempdb.dbo.#BOM') IS NOT NULL DROP TABLE #BOM
CREATE TABLE #BOM
(
in_Code NVARCHAR(60),
in_Date DATETIME,
Orders NVARCHAR(60),
BOM_materials NVARCHAR(60),
in_Qty DECIMAL(18,4)
)
INSERT INTO #BOM
(in_Code, in_Date, Orders, BOM_materials, in_Qty)
VALUES (N'RK0001', '2022-03-08', N'A01', N'L01', 50),
(N'RK0001', '2022-03-08', N'A01', N'L02', 30),
(N'RK0002', '2022-03-15', N'A01', N'L01', 50)
SELECT * FROM #BOM
in_Code | in_Date | Orders | BOM_materials | in_Qty |
---|---|---|---|---|
RK0001 | 2022-03-05 | A01 | L01 | 50 |
RK0001 | 2022-03-05 | A01 | L02 | 30 |
RK0002 | 2022-03-18 | A01 | L01 | 50 |
IF OBJECT_ID('tempdb.dbo.#picking') IS NOT NULL DROP TABLE #picking
CREATE TABLE #picking
(
out_Code NVARCHAR(60),
out_Date DATETIME,
Orders NVARCHAR(60),
materials NVARCHAR(60),
out_Qty DECIMAL(18,4),
out_ID BIGINT
)
INSERT INTO #picking
(out_Code, out_Date, Orders, materials, out_Qty, out_ID)
VALUES (N'CK0001', '2022-01-08', N'A01', N'L01', 90, 1),
(N'CK0002', '2022-01-20', N'A01', N'L01', 70, 2),
(N'CK0003', '2022-01-30', N'A01', N'L02', 10, 3)
SELECT * FROM #picking
out_Code | out_Date | Orders | materials | out_Qty | out_ID |
---|---|---|---|---|---|
CK0001 | 2022-01-08 | A01 | L01 | 90 | 1 |
CK0002 | 2022-01-20 | A01 | L01 | 70 | 2 |
CK0003 | 2022-01-30 | A01 | L02 | 10 | 3 |
期望结果
in_Code | in_Date | Orders | BOM_materials | in_Qty | out_Code | out_Qty | need_Qty | out_id |
---|---|---|---|---|---|---|---|---|
RK0001 | 2022-03-05 | A01 | L01 | 50 | CK0001 | 90 | 50 | 1 |
RK0001 | 2022-03-05 | A01 | L02 | 30 | CK0003 | 10 | 10 | 3 |
RK0002 | 2022-03-18 | A01 | L01 | 50 | CK0001 | 90 | 40 | 1 |
RK0002 | 2022-03-18 | A01 | L01 | 50 | CK0002 | 70 | 10 | 2 |
1
xuanbg 2022-12-21 00:08:19 +08:00
完全看不懂。。。
BOM 没有对应的工艺配料表?没有下料单?没有材料出库单?没有成品入库单?你这个期望结果财务能用? |
2
tkhlo 2022-12-21 10:46:20 +08:00
这是先进先出计算利润的方法,看下有没有参考价值:
select identity(int,1,1) ID,* into #1 from Tin order by 品种,时间 select 品种,sum(数量) 数量 into #2 from Tout group by 品种 select 品种,sum(数量) 数量,sum(数量*价格) 金额 from ( select a.品种, (case when (select sum(数量) from #1 where ID<=a.ID and 品种=a.品种)<= b.数量 then 0 else (case when (select isnull(sum(数量),0) from #1 where ID<a.ID and 品种=a.品种)<= b.数量 then (select sum(数量) from #1 where ID<=a.ID and 品种=a.品种)-b.数量 else a.数量 end) end) 数量, 价格 from #1 a,#2 b where a.品种=b.品种) a group by 品种 |