-2

我有以下表格:

Material
material_ID | material_Name
A           | Sugar
B           | Flour
C           | Egg Yolk
D           | Water
E           | Tea Powder

Product
product_ID  | product_Name | material_ID    
1           | Cake         | A              
1           | Cake         | B              
1           | Cake         | C
2           | Tea          | D
2           | Tea          | E
2           | Tea          | A              

ScaleData
record_ID   | product_ID     | material_ID  | Weight
1001        | 1              | A            | 30
1002        | 1              | B            | 11
1003        | 1              | C            | 25
1004        | 1              | A            | 31
1005        | 1              | B            | 25
1006        | 2              | D            | 15
1007        | 2              | E            | 20
1008        | 2              | E            | 21

如您所见:产品 1 是蛋糕,它需要材料:A、B 和 C 才能形成完整的产品。产品 2 是茶,它需要材料:D、E 和 A 才能形成完整的产品。

From the weighing table (ScaleData) we can see that there is 1 cake and 0 tea. 

record_ID: 1001, 1002, 1003 creates one complete cake.
record_ID: 1004, 1005 are remaining incomplete cake ingredients.
record_ID: 1006, 1007, 1008 are remaining incomplete tea ingredients.

Question:
A. How can i create the following result table based on data from above tables:

Result
product_ID  | product_Name  | Qty
1           | Cake          | 1
2           | Tea           | 0


B. How can i display the remaining ingredients like below?

Remaining
record_ID   | product_ID   | material_ID  | Weight
1004        | 1            | A            | 31
1005        | 1            | B            | 25
1006        | 2            | D            | 15
1007        | 2            | E            | 20
1008        | 2            | E            | 21

编辑:我目前能够通过混合使用 .NET 代码 + SQL 查询来解决这个问题。我正在寻找的是纯 SQL 查询解决方案。我目前的方法如下:

1. Execute: "SELECT DISTINCT product_ID FROM Product" to get a list of Product_ID.

2. Iterate through each product_ID and retrieve list of material from each product.
   Something like: "SELECT material_ID FROM Product WHERE product_ID=1"

3. Scan ScaleData table for each material and get the total row counts
   Something like:
   TotalA = "SELECT COUNT(*) FROM ScaleData WHERE product_ID=1 AND material_ID='A'
   TotalB = "SELECT COUNT(*) FROM ScaleData WHERE product_ID=1 AND material_ID='B'
   TotalC = "SELECT COUNT(*) FROM ScaleData WHERE product_ID=1 AND material_ID='C'

4. Compare variables: TotalA, TotalB, TotalC and get the lowest value.
   Lets say TotalA = 2 ; TotalB = 2 ; TotalC = 1
   LowestCount = TotalC = 1

5. Then we can tell total Qty for product 1 is 1 ( based on lowest count ).
   Remaining for Material A = 2 - 1 = 1
   Remaining for Material B = 2 - 1 = 1

这是我目前的解决方案,我知道它不是很有效。我更喜欢纯 SQL 解决方案,我希望一些 SQL 大师愿意帮助我..

4

1 回答 1

3

这将完成第一部分。它可能会被清理干净,但它基本上所做的就是采用每种产品中最少的成分,这就是您可以制造的产品数量。

SELECT product_id,product_name, min(c) Qty
FROM
(
  SELECT p.product_id, p.product_name, p.material_id, count(sd.product_id) c
  FROM product p
  LEFT JOIN scaledata sd
    ON p.product_id=sd.product_id
   AND p.material_id = sd.material_id
  GROUP BY product_id, p.product_name, p.material_id
) a
GROUP BY product_id

我制作了一个SQLfiddle 来测试任何有时间编写第二部分的人;)

于 2013-01-25T14:24:14.000 回答