我有三张桌子:
我遇到的问题是:
我有零件100831的销售订单,由100831-U制成。 我有一个工程订单号10表示100831,11表示100831-U。
我的查询结果是:
Part WO
100831 10
100831-U 11
这是正确的。
但我希望它是
Part WO
100831 10
100831 11
我只希望列出销售订单的零件及其所有零件的工作订单。 我尝试过使用变量,但我似乎无法做到正确。
非常感谢任何帮助
我创建了一个SQL小提琴 http://sqlfiddle.com/#!3/c77a5/1
目前的输出是:
| PART | WO |
|-----------|----|
| 100831 | 10 |
| 100831-U | 11 |
| 106061 | 12 |
| 43567 | 15 |
| R106061-1 | 13 |
| R106061-1 | 14 |
我试图在SO_Items(销售订单)中获取零件,其中包含BOM中的所有WO_No(工单号)。 (物料清单)
| PART | WO |
|-----------|----|
| 100831 | 10 |
| 100831 | 11 |
| 106061 | 12 |
| 43567 | 15 |
| 106061 | 13 |
| 106061 | 14 |
WITH SOCTE (Part) as
(
SELECT DISTINCT SO_Items.Part_No
FROM SO_Items
WHERE (SO_Items.Fully_Del='N')
),
CR (Part,Component) as
(
SELECT SOCTE.Part , Components.Component_Part_No
FROM Components
JOIN SOCTE
ON Components.Parent_Part_No = SOCTE.Part
UNION ALL
SELECT Parent_Part_No,Component_Part_No
FROM Components
INNER JOIN CR AS SCR ON SCR.Component = Components.Parent_Part_No
),
WOCTE(Part,WO) as
(
Select Distinct Part ,WO.WO_No
FROM WO
Inner JOIN CR
ON CR.Part=WO.Part_No and WO.WO_Complete=0 and WO.ReProcess=0
)
SELECT Part,WO FROM WOCTE
Order by Part
答案 0 :(得分:0)
好的,您的更新说明有很多帮助。
我想我拥有它:
;WITH SOCTE (Part) as (
SELECT DISTINCT SO_Items.Part_No
FROM SO_Items
WHERE (SO_Items.Fully_Del = 'N')
),
CR (Part, SO_Part, Component, Step) as (
SELECT SOCTE.Part, SOCTE.Part AS "SO_Part", Components.Component_Part_No, 0
FROM Components
JOIN SOCTE
ON Components.Parent_Part_No = SOCTE.Part
UNION ALL
SELECT Components.Parent_Part_No, SCR.SO_Part, Components.Component_Part_No, SCR.Step + 1
FROM Components
INNER JOIN CR AS SCR
ON SCR.Component = Components.Parent_Part_No
),
WOCTE (Part, WO) as (
SELECT DISTINCT CR.SO_Part, WO.WO_No
FROM WO
INNER JOIN CR
ON CR.Part = WO.Part_No
AND WO.WO_Complete = 0
AND WO.ReProcess = 0
)
SELECT *
FROM WOCTE
ORDER BY Part
这不完美,我在那里有额外的田地。我决定,如果你不能让一个领域做两件事,那么也许应该有第二个领域。 Step
字段只是为了查看CTE是如何递归的。使用带有步骤编号的SELECT * FROM CR
有助于查看字段的去向以及我需要保留的内容。