查询其他表字段和分组

时间:2017-06-04 15:05:45

标签: sql ms-access

我有一个查询,按天计算项目销售额(分组) 但我现在需要在此查询中包含同一查询中按天重新显示的总和数量 但是这个字段在其他表中

这是两个表的表示

SALES TABLE命名为" Item_Sales"

+------------+-------------+---------------+----------------+
| 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 Items Table名为" Items_Rec"

+-----------+------------+--------------+---------------+
| 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 |
+-----------+------------+--------------+---------------+

我有这个查询,但我需要从ITEMS_REC表中包含RecItemQty SUM

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 |
+------------+-------------+------------+--------------+----------------+

谢谢!

2 个答案:

答案 0 :(得分:0)

您需要加入两个表:

jsonString

答案 1 :(得分:0)

只需加入 ItemID ItemDate 上的两个聚合。下面使用两个派生表,但在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甚至两个UNION(对于FULL OUTER)如果项目记录在一个表中而不是另一个表中,您还需要捕获所有相关的行。