我一直在研究这个,还没有找到可用的答案。我是 SQL Server 的中度黑客,并且在函数和存储过程中使用参数方面取得了一些成功,但这是我似乎无法理解的组合。
为了清楚起见,我总结了以下情景:我的公司将计算机作为笔记本电脑、台式机和配件进行销售。我有一个 tbl_Computers,我在其中维护 Computer_Type、Model_Num 和 Mix_Percent,如下所示:
Desktop ABC .75
Desktop XYZ .25
Laptop DEF .60
Laptop MNO .40
我们还有一个按月预测的表格,我在其中维护 Computer_Type、Jul_Num、Aug_Num 和 Sep_Num,如下所示:
Desktop 100 200 150
Laptop 300 400 700
我为计划物料清单创建了一个功能,该功能将查找给定型号在过去 12 个月内销售的所有组件和配件。它的工作原理如下:
P_BOM ("ABC") 将生成一个包含两列的表:Component 和 Comp_Percent
CPU 1 (This means we sell 1 CPU with every desktop)
Hard Drive 2 (We sell 2 with every desktop)
Printer .8 (80% of the time we sell a printer)
我希望我的存储过程做的是提供一个看起来像这样的单个组合表,其中包含以下标题 Component、Jul_Num、Aug_Num 和 Sep_Num:
CPU 400 600 850
Hard Drive 500 800 1000
我通过总结以下逻辑得到 CPU 编号:
Desktop's Jul_Num x ABC's Mix_Percentage x ABC's CPU Comp_Percent
Desktop's Jul_Num x XYZ's Mix_Percentage x XYZ's CPU Comp_Percent
Laptop's Jul_Num x ABC's Mix_Percentage x ABC's CPU Comp_Percent
Laptop's Jul_Num x XYZ's Mix_Percentage x XYZ's CPU Comp_Percent
40 100 x .75 x 1) + (100 x .25 x 1) + (300 x .6 x 1) + (300 x .4 x 1)
有任何想法吗?
谢谢,
罗伯
编辑:
感谢建议这不必是一个迭代问题,而是一个基于表格的解决方案。
我创建了第一个表给我:
ABC CPU 3 3 1
ABC Hard Drive 6 3 2
DEF CPU 2 2 1
DEF Hard Drive 2 2 1
MNO CPU 1 1 1
MNO Hard Drive 1 1 1
XYZ CPU 1 1 1
XYZ Hard Drive 2 1 2
这是 SQL:
SELECT All_Components.Model_Num, All_Components.Part_Num, SUM(All_Components.Qty) AS Total, TTO.Target_Total, SUM(All_Components.Qty)
/ TTO.Target_Total AS Comp_Percent
FROM (SELECT Test_tbl_Computers.Model_Num, Test_tbl_Orders_2.Order_Num, Test_tbl_Orders_2.Part_Num, Test_tbl_Orders_2.Qty
FROM Test_tbl_Orders AS Test_tbl_Orders_2 CROSS JOIN
Test_tbl_Computers) AS All_Components INNER JOIN
(SELECT Test_tbl_Orders.Part_Num, SUM(Test_tbl_Orders.Qty) AS Target_Total
FROM Test_tbl_Orders INNER JOIN
Test_tbl_Computers AS Test_tbl_Computers_1 ON Test_tbl_Orders.Part_Num = Test_tbl_Computers_1.Model_Num
GROUP BY Test_tbl_Orders.Part_Num) AS TTO ON All_Components.Model_Num = TTO.Part_Num
WHERE (All_Components.Order_Num IN
(SELECT Order_Num
FROM Test_tbl_Orders AS Test_tbl_Orders_1
WHERE (Part_Num = All_Components.Model_Num))) AND (All_Components.Part_Num <> All_Components.Model_Num)
然后,为了防止它成为我无法驯服的 SQL 怪物,我创建了另一个函数来对预测和混合百分比进行内部连接,然后将所有按 Part_Num 分组的数字相加。
如果不出意外,我很感激必须写下我的问题以帮助集中我的想法。