MS ACCESS 交叉 table 数据
MS ACCESS Cross table DATA
我有两个 table,其中一个包含员工详细信息,如下所示
Employee ID | Employee Name | Start Date | Termination Date |
2234 | John Smith | 2014-01-03 | |
3333 | Jane Doe | 2014-04-22 | 2014-10-31 |
1234 | Bobby Wilson | 2013-12-10 | |
我的第二个 table 有销售额
看起来是这样
Employee ID | 2013-12 | 2014-01 | 2014-02 | 2014-03 | 2014-04 | etc | etc
2234 | | 199.99 | 130.00 | 300.00 | 230.99 | etc | etc
3333 | | | | | 204.02 | etc | etc
1234 | 455.99 | 332.32 | 334.00 | 553.00 | 334.99 | etc | etc
所以我需要做一个新的查询,帮助我根据第一个月的时间显示员工的第一个月雇员销售数字、第二个月销售额、第三个月销售额等的趋势当员工开始工作时。超过 12 个月或更长时间
所以新的 table 将如下所示
Employee | Month 1 | Month 2 | Month 3 | Month 4 | etc | etc |
2234 | 199.99 | 130.00 | 300.00 | 230.99 | etc | etc |
3333 | 240.02 | (month 2) | (month 3)| (month 4)| etc | etc |
1234 | 455.99 | 332.32 | 334.00 | 553.00 | etc | etc |
所以我知道我们可以通过员工 ID 加入两个 table
我知道我们可以根据员工的入职日期确定他们的第一个月
但是如何在 SQL 中写出这个查询?
正如我在对问题的评论中提到的,您需要更改第二个 table 的设计,如下所示(伪代码):
CREATE TABLE Sales(
SaleId Autonumber PK
[Employee ID] FK (reference to Employees table)
SalesDate DateTime
Sales Decimal/Double
);
然后你就可以这样保存你的数据了:
SaleID EmpId SalesDate Sales
1 2234 2014-01-01 199,99
2 1234 2013-12-01 455.99
3 1234 2014-01-01 332.32
4 2234 2014-02-01 130
5 1234 2014-02-01 334
6 2234 2014-03-01 300
7 1234 2014-03-01 553
8 2234 2014-04-01 230.99
9 3333 2014-04-01 204.02
10 1234 2014-04-01 334.99
最后,您的枢轴 table 可能如下所示:
TRANSFORM Sum(S.Sales) AS SumOfSales
SELECT E.[Employee Id], E.[Employee Name]
FROM Employees AS E INNER JOIN SalesByMY AS S
ON E.[Employee Id] = S.[Employee Id]
GROUP BY E.[Employee Id], E.[Employee Name]
PIVOT 'Month-' & DateDiff('m',[E].[StartDate],[S].[SalesDate])+1;
结果:
EmpId EmpName Month-1 Month-2 Month-3 Month-4 Month-5
1234 Bobby Wilson 455.99 332.32 334 553 334.99
2234 John Smith 199.99 130 300 230.99
3333 Jane Doe 204.02
我有两个 table,其中一个包含员工详细信息,如下所示
Employee ID | Employee Name | Start Date | Termination Date |
2234 | John Smith | 2014-01-03 | |
3333 | Jane Doe | 2014-04-22 | 2014-10-31 |
1234 | Bobby Wilson | 2013-12-10 | |
我的第二个 table 有销售额
看起来是这样
Employee ID | 2013-12 | 2014-01 | 2014-02 | 2014-03 | 2014-04 | etc | etc
2234 | | 199.99 | 130.00 | 300.00 | 230.99 | etc | etc
3333 | | | | | 204.02 | etc | etc
1234 | 455.99 | 332.32 | 334.00 | 553.00 | 334.99 | etc | etc
所以我需要做一个新的查询,帮助我根据第一个月的时间显示员工的第一个月雇员销售数字、第二个月销售额、第三个月销售额等的趋势当员工开始工作时。超过 12 个月或更长时间
所以新的 table 将如下所示
Employee | Month 1 | Month 2 | Month 3 | Month 4 | etc | etc |
2234 | 199.99 | 130.00 | 300.00 | 230.99 | etc | etc |
3333 | 240.02 | (month 2) | (month 3)| (month 4)| etc | etc |
1234 | 455.99 | 332.32 | 334.00 | 553.00 | etc | etc |
所以我知道我们可以通过员工 ID 加入两个 table 我知道我们可以根据员工的入职日期确定他们的第一个月 但是如何在 SQL 中写出这个查询?
正如我在对问题的评论中提到的,您需要更改第二个 table 的设计,如下所示(伪代码):
CREATE TABLE Sales(
SaleId Autonumber PK
[Employee ID] FK (reference to Employees table)
SalesDate DateTime
Sales Decimal/Double
);
然后你就可以这样保存你的数据了:
SaleID EmpId SalesDate Sales
1 2234 2014-01-01 199,99
2 1234 2013-12-01 455.99
3 1234 2014-01-01 332.32
4 2234 2014-02-01 130
5 1234 2014-02-01 334
6 2234 2014-03-01 300
7 1234 2014-03-01 553
8 2234 2014-04-01 230.99
9 3333 2014-04-01 204.02
10 1234 2014-04-01 334.99
最后,您的枢轴 table 可能如下所示:
TRANSFORM Sum(S.Sales) AS SumOfSales
SELECT E.[Employee Id], E.[Employee Name]
FROM Employees AS E INNER JOIN SalesByMY AS S
ON E.[Employee Id] = S.[Employee Id]
GROUP BY E.[Employee Id], E.[Employee Name]
PIVOT 'Month-' & DateDiff('m',[E].[StartDate],[S].[SalesDate])+1;
结果:
EmpId EmpName Month-1 Month-2 Month-3 Month-4 Month-5
1234 Bobby Wilson 455.99 332.32 334 553 334.99
2234 John Smith 199.99 130 300 230.99
3333 Jane Doe 204.02