2

UNION ALLを使用して2つのクエリから結果を取得していますが、データの行が失われています。どの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

なぜこれが起こっているのか、そして私に何ができるのか、何か考えはありますか?

4

2 に答える 2

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 に答える
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

orderTotalも句から削除しましたGROUP BY。必要なものではないと思うからです。

編集: OP コメントに基づいて更新されました。

于 2013-03-10T20:21:45.647 に答える