简体   繁体   中英

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    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?

I have only begun to use SQL recently so if you could explain your response that would be great!

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?

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

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) 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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