1

我想将这两个查询的结果放在一起,但不知道如何组合它们。目标是按产品获得每月的总销售额。我有店内购买和在线订单。这是从在线订单中获取每月总销售额的第一个查询:

SELECT YEAR( orderDate ) AS "SalesYear"
     , MONTH( orderDate ) AS "SalesMonth"
     , SUM( orderTotal ) AS "TotalSales"
     , products.productID 
FROM orders
INNER JOIN orderdetails ON orders.orderID = orderDetails.orderID
INNER JOIN products ON orderDetails.productID = products.productID
GROUP BY productID, YEAR( orderDate ) , MONTH( orderDate )
ORDER BY YEAR( orderDate ) , MONTH( orderDate )

这是从商店购买中按产品检索每月总销售额的查询:

SELECT YEAR( orderDate ) AS "SalesYear"
    , MONTH( orderDate ) AS "SalesMonth"
    , SUM( orderTotal ) AS "TotalSales"
    , products.productID 
FROM in_storepurchase
INNER JOIN instorepurchasedetails 
   ON in_storepurchase.isPurchaseID = instorepurchasedetails.isPurchaseID
INNER JOIN products 
   ON instorepurchasedetails.productID = products.productID
 GROUP BY productID, YEAR( orderDate ) , MONTH( orderDate )
 ORDER BY YEAR( orderDate ) , MONTH( orderDate )

任何有关如何将其纳入一个查询以便我可以在单个表上获得所有结果的帮助将不胜感激。

4

1 回答 1

1

当然,你可以使用 UNION:

SELECT 
    YEAR( t.orderDate ) AS "SalesYear", MONTH( t.orderDate ) AS "SalesMonth", SUM( t.orderTotal ) AS "TotalSales", productID
FROM
(
 select 1 
     UNION ALL
 select 2 
) as t
GROUP BY
    t.productID, YEAR( t.orderDate ) , MONTH( t.orderDate )
于 2013-03-10T18:13:57.990 回答