2

使用 UNION ALL 从两个查询中获取结果,但它正在丢失一行数据。如果我切换哪个选择语句首先出现,则丢失的数据会切换到不同的行。我知道每个 slect 语句在单独运行时都会提取正确的信息,但放在一起时不能正常工作。以下是我的查询:

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

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

任何想法为什么会发生这种情况以及我能做什么?

4

2 回答 2

1

您需要为GROUP BY每个块添加一个子句SELECT,并指定正确的列,如下所示:

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

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

我还从子句中删除了orderTotalGROUP BY ,因为我认为这不是您需要的。

编辑:根据 OP 评论更新。

于 2013-03-10T20:21:45.647 回答
1
SELECT `x`.`SalesYear`, 
`x`.`SalesMonth`, 
SUM( `x`. `TotalSales`) AS "TotalSales", 
productID

FROM
(

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

UNION ALL

     SELECT YEAR( orderDate ) AS "SalesYear", 
     MONTH( orderDate ) AS "SalesMonth", 
     SUM( orderTotal ) AS "TotalSales", 
     instorepurchasedetails.productID 
    FROM in_storepurchase
    INNER JOIN instorepurchasedetails ON in_storepurchase.isPurchaseID =                       instorepurchasedetails.isPurchaseID
    INNER JOIN products ON instorepurchasedetails.productID = products.productID
    GROUP BY YEAR(orderDate ) , MONTH( orderDate ), productID 
) x
     GROUP BY  `x`.`SalesMonth`, productID 
     ORDER BY `x`.`SalesMonth` ASC
于 2013-03-11T15:01:05.990 回答