我想将这两个查询的结果放在一起,但无法弄清楚如何将它们组合起来。目标是按产品获得每月的总销售额。我有店内购买和在线订单。这是第一个从在线订单中获得每月总销售额的查询:
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 )
有关如何将其纳入一个查询的任何帮助,以便我可以在一张桌子上获得所有结果,我们将不胜感激。
答案 0 :(得分: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 )