2017-06-04 76 views
0

我有一個查詢,按天(分組) 總結項目銷售額,但我現在需要在該查詢中包括在同一查詢 白天recibed總數量,但此字段是其他表查詢總和其他表字段和分組

這是兩個表的表示

Sales表名爲 「Item_Sales」 命名爲 「Items_Rec」

+------------+-------------+---------------+----------------+ 
| SaleItemID | SaleItemQty | SaleItemPrice | SaleItemDate | 
+------------+-------------+---------------+----------------+ 
|  51026 |  18.00 |   11.31 |  07/01/1016 | 
+------------+-------------+---------------+----------------+ 
|  14309 |  7.00 |   22.35 |  07/01/2016 | 
+------------+-------------+---------------+----------------+ 
|  51026 |  6.00 |   11.31 |  07/01/2016 | 
+------------+-------------+---------------+----------------+ 
|  51026 |  10.00 |   11.31 |  07/02/2016 | 
+------------+-------------+---------------+----------------+ 
|  14309 |  14.00 |   22.35 |  07/02/2016 | 
+------------+-------------+---------------+----------------+ 
|  14309 |  9.00 |   22.35 |  07/02/2016 | 
+------------+-------------+---------------+----------------+ 

Recibed項目表

+-----------+------------+--------------+---------------+ 
| RecItemID | RecItemQty | RecItemPrice | RecItemDate | 
+-----------+------------+--------------+---------------+ 
| 51026 |  10.00 |  11.31 | 07/01/1016 | 
+-----------+------------+--------------+---------------+ 
| 14309 |  5.00 |  22.35 | 07/01/2016 | 
+-----------+------------+--------------+---------------+ 
| 51026 |  5.00 |  11.31 | 07/01/2016 | 
+-----------+------------+--------------+---------------+ 
| 51026 |  10.00 |  11.31 | 07/02/2016 | 
+-----------+------------+--------------+---------------+ 
| 14309 |  5.00 |  22.35 | 07/02/2016 | 
+-----------+------------+--------------+---------------+ 
| 14309 |  5.00 |  22.35 | 07/02/2016 | 
+-----------+------------+--------------+---------------+ 

我有這個疑問,但我需要包括RecItemQty SUM從ITEMS_REC表

SELECT 
    S.SaleItemID, 
    Format(SUM(S.SaleItemQty), "###,##0.00")AS 'Quantity', 
    Format(SUM(S.SaleItemPrice), "###,##0.00")AS 'Amount', 
    S.SaleItemDate 
FROM Items_Sales 
GROUP BY S.SaleItemID,S.SaleItemDate 

這是結果,我需要

+------------+-------------+------------+--------------+----------------+ 
| SaleItemID | SaleItemQty | RecItemQty |SaleItemPrice | SaleItemDate | 
+------------+-------------+------------+--------------+----------------+ 
|  51026 |  24.00 |  15.00 |  11.31 |  07/01/1016 | 
+------------+-------------+------------+--------------+----------------+ 
|  14309 |  7.00 |  5.00 |  22.35 |  07/01/1016 | 
+------------+-------------+------------+--------------+----------------+ 
|  51026 |  10.00 |  10.00 |  11.31 |  07/02/1016 | 
+------------+-------------+------------+--------------+----------------+ 
|  14309 |  23.00 |  10.00 |  22.35 |  07/02/1016 | 
+------------+-------------+------------+--------------+----------------+ 

謝謝!

+0

什麼是'Recibed'?兩個表中有相同的SaleItemID和日期?構建兩個聚合查詢,然後在SaleItemID和兩個日期字段 - 複合連接上連接另一個查詢。爲什麼兩張桌子?以示例數據爲例,可以是1個表格。 – June7

回答

0

只要加入這兩個聚集在項目IDItemDate。下面使用兩個派生表,但在MS Access中,您可以將這兩個聚合查詢另存爲單獨的存儲查詢(即視圖),並在另一個查詢中將其加入。

SELECT subSales.SaleItemID, subSales.SumSaleItemQty, subRec.SumRecItemQty, 
     subSales.SumSaleItemPrice, subSales.SaleItemDate 
FROM 

    (SELECT 
     S.SaleItemID, 
     Format(SUM(S.SaleItemQty), "###,##0.00") AS SumSaleItemQty, 
     Format(SUM(S.SaleItemPrice), "###,##0.00") AS SumSaleItemPrice, 
     S.SaleItemDate 
    FROM Items_Sales S 
    GROUP BY S.SaleItemID, S.SaleItemDate) As subSales 

INNER JOIN 

    (SELECT 
     R.RecItemID, 
     Format(SUM(R.RecItemQty), "###,##0.00") AS SumRecItemQty, 
     Format(SUM(R.RecItemPrice), "###,##0.00") AS SumRecItemPrice, 
     R.RecItemDate 
    FROM Items_Rec R 
    GROUP BY R.RecItemID, R.RecItemDate) As subRec 

ON subSales.SaleItemID = subRec.RecItemID 
AND subSales.SaleItemDate = subRec.RecItemDate 

此外,考慮LEFT JOINRIGHT JOIN甚至兩個(爲FULL OUTER)UNION的情況下,項目的記錄是在一個表中,而不是其他,你需要靜像拍攝的所有相關行。

0

你需要加入兩個表:

SELECT 
    S.SaleItemID, 
    Format(SUM(R.RecItemQty), "###,##0.00")AS 'Received' 
    Format(SUM(S.SaleItemQty), "###,##0.00")AS 'Quantity', 
    Format(SUM(S.SaleItemPrice), "###,##0.00")AS 'Amount', 
    S.SaleItemDate 
FROM Items_Sales S 
INNER JOIN Items_Rec R 
    ON S.SaleItemID = R.RecItemID 
    AND S.SalesItemDate = R.RecItemDate 
GROUP BY S.SaleItemID,S.SaleItemDate 
+0

警告:如果有多個匹配記錄,則此單位級連接可能會重複求和。你想加入他們的聚合。另外,你不加入日期。 – Parfait