我有一個查詢,按天(分組) 總結項目銷售額,但我現在需要在該查詢中包括在同一查詢 白天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 |
+------------+-------------+------------+--------------+----------------+
謝謝!
什麼是'Recibed'?兩個表中有相同的SaleItemID和日期?構建兩個聚合查詢,然後在SaleItemID和兩個日期字段 - 複合連接上連接另一個查詢。爲什麼兩張桌子?以示例數據爲例,可以是1個表格。 – June7