SQL选择查询到另一个查询
我是SQL的初学者,所以如果我的术语/编码有点偏离,或者可能偏离太远,请提前道歉。 我有两个问题想合并成一个问题。第一种方法是创建包含两个特定进程的ProductID列表。 然后我想在第二个查询中使用这些ProductID 下面是正确的吗? 按产品分组。productid具有(sum(当processid类似于'pick%'时为1 else 0 end)+sum(当processid类似于'pack%'时为1 else 0 end时为1 else 0 end)=2) 任何帮助都将不胜感激,希望这是有意义的SQL选择查询到另一个查询,sql,select,Sql,Select,我是SQL的初学者,所以如果我的术语/编码有点偏离,或者可能偏离太远,请提前道歉。 我有两个问题想合并成一个问题。第一种方法是创建包含两个特定进程的ProductID列表。 然后我想在第二个查询中使用这些ProductID 下面是正确的吗? 按产品分组。productid具有(sum(当processid类似于'pick%'时为1 else 0 end)+sum(当processid类似于'pack%'时为1 else 0 end时为1 else 0 end)=2) 任何帮助都将不胜感激,希望这
SELECT
Products.ProductID
FROM Products
INNER JOIN Categories ON Products.Category = Categories.Category
INNER JOIN Boms ON Products.Product = Boms.Product AND Boms.BomVersion = Products.BomVersion
LEFT OUTER JOIN Products AS Comp ON Boms.Component = Comp.Product
LEFT OUTER JOIN Processes ON Boms.Process = Processes.Process
WHERE
products.active = 1
and Categorys.Categoryid in ('5','20')
group by products.productid
having (sum(case when processid like 'pick%' then 1 else 0 end) + sum(case when processid like 'pack%' then 1 else 0 end) = 2)
order by products.productid
SELECT
Products.ProductID,
Products.productdescription,
Boms.Type As Type,
Comp.ProductId as Component,
Comp.productdescription,
Boms.Quantity,
BomVersions.BomVersionID,
Processes.processid,
Processes.ProcessDescription
FROM Products
INNER JOIN Categories ON Products.Category = Categories.Category
INNER JOIN Boms ON Products.Product = Boms.Product AND Boms.BomVersion = Products.BomVersion
LEFT OUTER JOIN Products AS Comp ON Boms.Component = Comp.Product
LEFT OUTER JOIN Processes ON Boms.Process = Processes.Process
INNER JOIN BomVersions ON Products.BomVersion = BomVersions.BomVersion
WHERE
products.active = 1
order by products.productid, products.type,comp.productid
要组合它们,可以执行以下操作
SELECT b.*
FROM
(SELECT Products.ProductID FROM Products INNER JOIN ...) AS a
INNER JOIN
(SELECT Products.ProductID, Products.productdescription, Boms.Type As Type, ...) AS b
ON a.ProductID = b.ProductID
要组合它们,可以执行以下操作
SELECT b.*
FROM
(SELECT Products.ProductID FROM Products INNER JOIN ...) AS a
INNER JOIN
(SELECT Products.ProductID, Products.productdescription, Boms.Type As Type, ...) AS b
ON a.ProductID = b.ProductID
它返回正确的数据吗?它返回正确的数据吗?