我是否需要日历才能按月加入访问和报告中的表
Do I need a calendar to join tables in access and report by month
我有 3 个 table 需要加入 Access。因此,我创建了查询以获取 "Team" 字段,并在 "Assoc. NO" 上使用 [Table A] 到 [Table C] 的内部连接,然后在 [[=25] 上获取另一个字段=] B] 到 [Table C] 也在“Assoc. NO”上。
我的问题
我使用基于 [Table C] 和 [Table A] 的查询来获取月份,但是如果月份不存在于 [Table A] 并且只有 [Table B]。我是否需要创建一个只有几个月和几年的 table 并创建一个连接?对于冗长的 post,我深表歉意,但我想提供尽可能多的信息。在此先感谢您的帮助。
Table A
|Name | Assoc. NO |month|Year|Product|SaleA|
|John Smith | 1 |Jan |2016|Apple | |
|John Smith | 1 |Jan |2016|Pear | |
|John Smith | 1 |Feb |2016|Apple | |
|George Martin| 2 |Feb |2016|Apple | |
|George Martin| 2 |Feb |2016|Apple | |
|George Martin| 2 |Feb |2016|Pear | |
Table B
|Name | Assoc. NO |month|Year|Service |SaleB|
|John Smith | 1 |Jan |2016|oil change| |
|George Martin| 2 |Jan |2016|oil change| |
|Mark James | 3 |Feb |2016|oil change| |
|Mark James | 3 |Mar |2016|oil change| |
|George Martin| 2 |Mar |2016|oil change| |
Table C
|Team |Name | Assoc. NO |
|Team A |John Smith | 1 |
|Team B |George Martin| 2 |
|Team B |Mark James | 3 |
我想看的是:
Query
|Team |Name | Month |Sale |SaleB|SUM(SaleA,SaleB)|
|-------|-------------|--------|-----|-----|----------------|
|Team A |John Smith | Jan | | | |
|Team A |John Smith | Feb | | [=11=] | |
|Team B |George Martin| Jan | [=11=] | | |
|Team B |George Martin| Feb | | [=11=] | |
|Team B |George Martin| Mar | [=11=] | | |
|Team C |Mark James | Feb | [=11=] | | |
|Team C |Mark James | Feb | [=11=] | | |
SELECT [Table C].Team, [Table C].Name, [Table C].[Assoc No], Month, Sum(T.SaleA) AS TotA, Sum(T.SaleB) AS TotB, [TotA]+[TotB] AS Total
FROM
(select [Assoc no], Month, SaleA, 0 as SaleB from [Table A]
UNION ALL select [Assoc No], Month, 0, Saleb from [Table B]) AS T
INNER JOIN [Table C] ON T.[Assoc No]= [Table C].[Assoc No]
GROUP BY [Table C].Team, [Table C].Name, Table C].[Assoc No], T.Month;
我有 3 个 table 需要加入 Access。因此,我创建了查询以获取 "Team" 字段,并在 "Assoc. NO" 上使用 [Table A] 到 [Table C] 的内部连接,然后在 [[=25] 上获取另一个字段=] B] 到 [Table C] 也在“Assoc. NO”上。
我的问题
我使用基于 [Table C] 和 [Table A] 的查询来获取月份,但是如果月份不存在于 [Table A] 并且只有 [Table B]。我是否需要创建一个只有几个月和几年的 table 并创建一个连接?对于冗长的 post,我深表歉意,但我想提供尽可能多的信息。在此先感谢您的帮助。
Table A
|Name | Assoc. NO |month|Year|Product|SaleA|
|John Smith | 1 |Jan |2016|Apple | |
|John Smith | 1 |Jan |2016|Pear | |
|John Smith | 1 |Feb |2016|Apple | |
|George Martin| 2 |Feb |2016|Apple | |
|George Martin| 2 |Feb |2016|Apple | |
|George Martin| 2 |Feb |2016|Pear | |
Table B
|Name | Assoc. NO |month|Year|Service |SaleB|
|John Smith | 1 |Jan |2016|oil change| |
|George Martin| 2 |Jan |2016|oil change| |
|Mark James | 3 |Feb |2016|oil change| |
|Mark James | 3 |Mar |2016|oil change| |
|George Martin| 2 |Mar |2016|oil change| |
Table C
|Team |Name | Assoc. NO |
|Team A |John Smith | 1 |
|Team B |George Martin| 2 |
|Team B |Mark James | 3 |
我想看的是:
Query
|Team |Name | Month |Sale |SaleB|SUM(SaleA,SaleB)|
|-------|-------------|--------|-----|-----|----------------|
|Team A |John Smith | Jan | | | |
|Team A |John Smith | Feb | | [=11=] | |
|Team B |George Martin| Jan | [=11=] | | |
|Team B |George Martin| Feb | | [=11=] | |
|Team B |George Martin| Mar | [=11=] | | |
|Team C |Mark James | Feb | [=11=] | | |
|Team C |Mark James | Feb | [=11=] | | |
SELECT [Table C].Team, [Table C].Name, [Table C].[Assoc No], Month, Sum(T.SaleA) AS TotA, Sum(T.SaleB) AS TotB, [TotA]+[TotB] AS Total
FROM
(select [Assoc no], Month, SaleA, 0 as SaleB from [Table A]
UNION ALL select [Assoc No], Month, 0, Saleb from [Table B]) AS T
INNER JOIN [Table C] ON T.[Assoc No]= [Table C].[Assoc No]
GROUP BY [Table C].Team, [Table C].Name, Table C].[Assoc No], T.Month;