【发布时间】:2020-03-14 15:14:22
【问题描述】:
我正在尝试创建一个查询来汇总每周销售额。我有两个销售团队(可能更及时)和销售人员分配给每个团队。我需要计算每个团队和团队内部的销售百分比。
我想要一个永久查询而不是生成 VBA。我知道我可以使用 VB 生成的 CREATE 和 INSERT 语句创建一个 Temp 表,但该表不会随着信息添加到 sales 表而更新。
创建此查询后,我将需要再构造至少两个查询,其中计算来自该数据的百分比,因为在打开表单时需要该信息。
结果查询应该输出类似于以下示例的内容,考虑到可以在 WHERE 子句中从第 1 周到第 Y 周过滤周范围,并且 SALESTEAM 表中的所有 ID_SALES(从 1 到 X)行都是一列自己的。
This are the tables to extract the data from
TABLE Sales
ID as AutoNumber
ID_CUSTOMER AS Number
ID_SALES as Number
SaleDate as Date/Time
SalesAmt as Currency
END TABLE
TABLE SALESTEAM
ID_SALES as AutoNumber
Name as String
StartDate as Date/Time
EndDate as Date/Time
Active as Yes/No
AssignedTo as Number
END TABLE
This would be a sample construct of the SELECT if made statically for the current records in SalesTeam Table using the All ID_Sales from all records
SELECT DatePart("yyyy", SalesDate) as SalesYear, DatePart("ww", SalesDate) as SalesWeek,
(SUM(SalesAmt) FROM SALES WHERE ID_Sales = 1 AND SalesDate BETWEEN @StartDate AND @EndDate) as Sales1,
(SUM(SalesAmt) FROM SALES WHERE ID_Sales = 2 AND SalesDate BETWEEN @StartDate AND @EndDate) as Sales2,
(SUM(SalesAmt) FROM SALES WHERE ID_Sales = 3 AND SalesDate BETWEEN @StartDate AND @EndDate) as Sales3,
...
(SUM(SalesAmt) FROM SALES WHERE ID_Sales = X AND SalesDate BETWEEN @StartDate AND @EndDate) as SalesX
FROM Sales GROUP BY SalesYear, SalesWeek ORDER BY SalesYear, SalesWeek
+------+----------------------------------------------------------------------+
| WEEK | ID_SALES |
+------+----------------------------------------------------------------------+
| WEEK | 1 | 2 | ... | X |
+------+------------------+------------------+-------------+------------------+
+ W1 | SUM(SALESAMT) | SUM(SALESAMT) | ... | SUM(SALESAMT) |
+ | WK1 ID_SALES = 1 | WK1 ID_SALES = 2 | ... | WK1 ID_SALES = X |
+------+------------------+------------------+-------------+------------------+
| | | | | |
+------+------------------+------------------+-------------+------------------+
+ WY | SUM(SALESAMT) | SUM(SALESAMT) | ... | SUM(SALESAMT) |
+ | WKY ID_SALES = 1 | WKY ID_SALES = 2 | ... | WKY ID_SALES = X |
+------+------------------+------------------+-------------+------------------+
【问题讨论】:
-
你的问题是什么?您似乎已经有一个查询正在执行此操作。
-
SalesTeam 表将及时增加,因为最终用户将无法自行更改查询。这需要是动态的。
-
不知道这两张表的schema很难给你答案。
-
架构已编辑为原始帖子
-
我认为您可以使用Crosstab Query 来做到这一点。您可以用 SQL 编写或在查询设计器中编写。
标签: ms-access select sum multiple-columns