UNION ALL查询丢失的数据?

时间:2013-03-10 20:01:59

标签: mysql

使用UNION ALL从两个查询中获取结果,但它丢失了一行数据。如果我首先切换哪个select语句,则丢失的数据将切换到另一行。我知道每个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

为什么会发生这种情况以及我能做些什么呢?

2 个答案:

答案 0 :(得分:0)

您需要为每个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

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

编辑:根据OP评论更新。

答案 1 :(得分:0)

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