简体   繁体   English

如何合并两个查询?

[英]How to combine two queries?

I need to combine the two following queries... 我需要结合以下两个查询...

SELECT     Products.ProductId, Products.ProductDescription, SUM(Inventory.QuantityOutstanding) AS Inventory, Products.AverageCost AS Cost

FROM         Products INNER JOIN
                  Inventory ON Products.Product = Inventory.Product
WHERE    (Inventory > 0) AND (Products.ProductId LIKE 'CAS%') OR
                  (Products.ProductId LIKE 'ASY%')
GROUP BY Products.ProductId, Products.ProductDescription, Products.AverageCost
ORDER BY Products.ProductId

Which gives a table like... 这给出了像...的表格

ProductID    ProductDescription    Inventory    Cost
-------------------------------------------------------
   AB            CD                  0???       0
   UV            XY                    5        555
   .             .                     .         .
   .             .                     .         .
   .             .                     .         .

And

SELECT Components.ProductId, SUM(SalesOrderItems.QuantityOutstanding) AS Schedule

FROM Structures INNER JOIN
Products AS Components ON  Structures.Component = Components.Product INNER JOIN
Products AS Products ON Products.Product = Structures.Product AND 
Structures.StructureVersion = Products.StructureVersion LEFT OUTER JOIN
SalesOrders INNER JOIN
SalesOrderItems ON SalesOrders.SalesOrder = SalesOrderItems.SalesOrder ON 
Products.Product = SalesOrderItems.Product


WHERE      ((Components.ProductId LIKE 'CAS%') OR (Components.ProductId LIKE 'ASY%')) AND (SalesOrderItems.DueDate < DATEADD(m, 3, GETDATE())) AND (SalesOrderItems.QuantityOutstanding > 0)
GROUP BY Components.ProductId, Products.ProductId
ORDER BY Components.ProductId

Which gives a table like... 这给出了像...的表格

ProductId      Schedule
-------------------------
  AB             360
  UV             3
   .             .
   .             .
   .             .

I basically want to have a table that displays the ProductId, (Inventory - Schedule) AS XSStock, and Cost like this... 我基本上想要一个表来显示ProductId,(库存-计划)AS XSStock和这样的成本...

ProductId    XSStock (>0 only)    Cost
-------------------------------------------
   UV             2                222
   .              .                 .
   .              .                 .
   .              .                 .

I thought this may be UNION or a subquery but I can't seem to make either work? 我以为这可能是UNION或子查询,但是我似乎无法使它们正常工作?

I have only begun to use SQL recently so if you could explain your response that would be great! 我最近才开始使用SQL,因此,如果您能解释一下您的回答,那就太好了!

KATIA EDIT QUERY: 卡蒂亚编辑查询:

SELECT ProductId, ProductDescription, Inventory, Cost, SUM(Orders) AS Demand, (Inventory - SUM(Orders)) AS XSStock

FROM (SELECT X.ProductId, X.ProductDescription, X.Inventory, X.Cost, SUM(Y.Schedule) AS Orders

    FROM (SELECT     Products.ProductId, Products.ProductDescription, SUM(Inventory.QuantityOutstanding) AS Inventory, Products.AverageCost AS Cost

        FROM         Products INNER JOIN
                            Inventory ON Products.Product = Inventory.Product
        WHERE     (Products.ProductId LIKE 'CAS%') OR
                            (Products.ProductId LIKE 'ASY%')
        GROUP BY Products.ProductId, Products.ProductDescription, Products.AverageCost) AS X, 

    (SELECT Components.ProductId, SUM(SalesOrderItems.QuantityOutstanding) AS Schedule

        FROM Structures INNER JOIN
        Products AS Components ON  Structures.Component = Components.Product INNER JOIN
        Products AS Products ON Products.Product = Structures.Product AND 
        Structures.StructureVersion = Products.StructureVersion LEFT OUTER JOIN
                SalesOrders INNER JOIN
        SalesOrderItems ON SalesOrders.SalesOrder = SalesOrderItems.SalesOrder ON 
        Products.Product = SalesOrderItems.Product

    WHERE      ((Components.ProductId LIKE 'CAS%') OR (Components.ProductId LIKE 'ASY%')) AND (SalesOrderItems.DueDate < DATEADD(m, 3, GETDATE())) AND (SalesOrderItems.QuantityOutstanding > 0)
    GROUP BY Components.ProductId, Products.ProductId) AS Y


WHERE (Y.ProductId LIKE X.ProductId)
GROUP BY X.ProductId, X.ProductDescription, X.Inventory, X.Cost)

WHERE ((Inventory - SUM(Orders)) > 0)
GROUP BY ProductId, ProductDescription, Inventory, Cost

ORDER BY ProductId

I am now getting error message... 我现在收到错误消息...

Incorrect syntax near the keyword 'WHERE'

This is in line 30, but I don't know why? 这是第30行,但我不知道为什么?

您也可以尝试对两个查询都使用别名并在选择查询中选择它们

SELECT x.a, y.b FROM (SELECT * from a) as x, (SELECT * FROM b) as y

To fix Inventory = 0 you just need to add brackets here: 要修复Inventory = 0您只需要在此处添加括号:

`(Inventory > 0) AND *((Products.ProductId LIKE 'CAS%') OR (Products.ProductId LIKE 'ASY%'))`

About query: why don't you want to make selection from Products and join everything there? 关于查询:您为什么不想从“产品”中进行选择并加入其中的所有内容? You have ProductsId in all tables. 您在所有表中都有ProductsId。 I tried to combine it into one, maybe it will help you: 我尝试将其合并为一个,也许它将对您有所帮助:

SELECT     Products.ProductId, Products.ProductDescription, SUM(Inventory.QuantityOutstanding) AS Inventory, Products.AverageCost AS Cost
 SUM(SalesOrderItems.QuantityOutstanding) AS Schedule
from Products

inner join Inventory ON Products.Product = Inventory.Product
inner join structures AS Components ON  Structures.Component = Products.Product INNER JOIN
inner join structurs as structures.Product = Product.Product and
Structures.StructureVersion = Products.StructureVersion 
LEFT OUTER JOIN
SalesOrders INNER JOIN SalesOrderItems ON SalesOrders.SalesOrder = SalesOrderItems.SalesOrder ON 
Products.Product = SalesOrderItems.Product
WHERE    (Inventory > 0) AND ((Products.ProductId LIKE 'CAS%') OR (Products.ProductId LIKE 'ASY%'))
and  ((Products.ProductId LIKE 'CAS%') OR (Products.ProductId LIKE 'ASY%')) 
AND (SalesOrderItems.DueDate < DATEADD(m, 3, GETDATE())) AND (SalesOrderItems.QuantityOutstanding > 0)

GROUP BY Products.ProductId, Products.ProductDescription, Products.AverageCost
ORDER BY Products.ProductId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM