1

我正在尝试开发一个相当简单的采购订单跟踪数据库。我无法想象最有效的方法是什么。

目前我有4张桌子:

Purchase Order table:
PO Number, Customer Product Number, Quantity
6874     , ABC-0001                 4
6873     , XYX-2222                 1

Customer Product Table:
Customer Product Number, Finished Goods Number
ABC-0001               , 501-123
ABC-0001               , 501-124
ABC-0001               , 501-125


Finished Goods Table:
Finished Goods Number, Component Number, QTY Per FG
501-123              , COMP-0001       , 1
501-123              , COMP-0004       , 16
501-123              , COMP-0009       , 12
501-124              , COMP-0005       , 5
501-124              , COMP-0003       , 9
501-124              , COMP-0001       , 10
501-125              , COMP-0006       , 3
501-125              , COMP-0004       , 2
501-125              , COMP-0003       , 1

Component Table, Suppplier ID, (etc. etc. etc.)
COMP-0001
COMP-0002
COMP-0003
COMP-0004
COMP-0005
COMP-0006
COMP-0007
COMP-0008
COMP-0009
COMP-0010

我需要生成一个列出特定订单所需的所有单个组件的列表。所以列表看起来像这样:

COMP-0001 - 44 (order quantity = 4 * (1 in 501-123 + 10 in 501-124)

等等等等

这可以仅在 SQL 中完成,还是我必须使用游标分步执行并在步骤之间生成中间表?这似乎是一件非常简单的事情,但我还没有找到一个如何做到这一点的例子。

桌子相当大。

Order table is around 6000 orders (some of which are already complete, typically 300 or so currently open)
Customer Product Table around 2000 items
Finished Goods around 2000
The component table contains over 20,000 separate components

我是否以正确的方式来实现结果?

感激地收到任何帮助。

4

3 回答 3

0

A JOIN/GROUP BY似乎是您需要的;

SELECT po.[PO Number], fg.[Component Number],
       SUM([Quantity]*[QTY Per FG]) Quantity
FROM PurchaseOrder po
JOIN CustomerProduct cp 
  ON po.[Customer Product Number]=cp.[Customer Product Number]
JOIN FinishedGoods fg 
  ON cp.[Finished Goods Number] = fg.[Finished Goods Number]
GROUP BY po.[PO Number], fg.[Component Number]

一个用于测试 SQL Server 的 SQLfiddle,但除了引用表名之外,应该没有任何特定的 RDBMS。

于 2013-08-07T06:49:46.023 回答
0

此查询应为您提供所需的结果:

SELECT C.COMPONENT , O.QUANTITY * (SUM(FG.QTY))
FROM ORDER O JOIN PRODUCT P ON O.CUSTOMER = P.CUSTOMER
JOIN FINISHED_GOODS FG ON P.FINISHED_GOODS = FG.FINISHED_GOODS
JOIN COMPONENT C ON FG.COMPONENT = C.COMPONENT
WHERE P.PO_NUMBER = ?
于 2013-08-07T06:50:16.700 回答
0
select
    po.[PO Number], fg.[Component Number],
    sum(po.[Quantity] * fg.[QTY Per FG]) as Quantity
from PurchaseOrder as po
    inner join CustomerProduct as cp on cp.[Customer Product Number] = po.[Customer Product Number]
    inner join FinishedGoods as fg on fg.[Finished Goods Number] = cp.[Finished Goods Number]
group by po.[PO Number], fg.[Component Number]
于 2013-08-07T06:55:11.003 回答