复杂查询MSACCESS

时间:2017-05-26 15:35:55

标签: sql ms-access

我有一个名为Sales的MS ACCESS表,它包含5个字段,需要创建一个查询来获取显示每个项目销售情况的报告。 表示例..

销售表

+------------+--------+---------+-----------+-----------+
|   SaleDate | ItemID | SaleQty | SalePrice | SaleTotal |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1105 |   30.00 |      5.25 |    157.50 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1105 |    2.00 |      5.25 |     10.50 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1105 |   10.00 |      6.00 |     60.00 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1105 |    4.00 |      6.00 |     24.00 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1106 |    3.00 |      8.00 |     24.00 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1106 |    8.00 |      8.00 |     64.00 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1106 |   12.00 |      8.50 |    102.00 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1106 |    6.00 |      8.50 |     51.00 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1105 |   12.00 |      6.00 |     72.00 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1105 |    9.00 |      6.00 |     54.00 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1105 |    1.00 |      6.50 |      6.50 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1105 |    2.00 |      6.50 |     13.00 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1106 |    7.00 |      8.50 |     59.50 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1106 |    4.00 |      8.50 |     34.00 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1106 |    3.00 |      8.70 |     26.10 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1106 |   16.00 |      8.70 |    139.20 |
+------------+--------+---------+-----------+-----------+`

这很难解释..但我试试 在表中存在两个记录(第一个)

实施例: 表中的前两个记录具有相同的SaleDate,ItemID和SalePrice

我需要做的是将SaleQty字段与具有相同SaleDate,ItemID和SalePrice的所有记录进行对比 (同样是SaleTotal)

销售查询结果(需要)

+------------+--------+---------+-----------+-----------+
|   SaleDate | ItemID | SaleQty | SalePrice | SaleTotal |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1105 |   32.00 |      5.25 |    168.00 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1105 |   14.00 |      6.00 |     10.50 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1106 |   11.00 |      8.00 |     88.00 |
+------------+--------+---------+-----------+-----------+
| 02/25/2017 |   1106 |   18.00 |      8.50 |    153.00 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1105 |   21.00 |      6.00 |    126.00 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1105 |    3.00 |      6.50 |     19.50 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1106 |   11.00 |      8.50 |     93.50 |
+------------+--------+---------+-----------+-----------+
| 03/25/2017 |   1106 |   19.00 |      8.70 |    165.30 |
+------------+--------+---------+-----------+-----------+`

谢谢..

1 个答案:

答案 0 :(得分:2)

试试这个

SELECT SaleDate, ItemID, SalePrice, SUM(SaleQty) AS SaleQty, SUM(SaleTotal) AS SaleTotal

FROM Sales

GROUP BY SaleDate, ItemID, SalePrice

要在MS Access中详细了解分组,请参阅https://support.office.com/en-gb/article/GROUP-BY-Clause-84eeb766-25d2-4aa1-8eea-002bb65ef3a0