访问交叉表小计列

时间:2013-04-24 20:55:59

标签: ms-access crosstab

我对Access中的交叉表有以下问题: 如何创建小计列?

我希望在查询结果中看到的是:

          Nov 2010     Dec 2010     2010 Total     Jan 2011    Feb 2011 
Row1             2            4             17            3           2
Row2             8            6             35            7           5

如何创建这一年的小计? (没关系,如果年份数据将在最后几个月之后) 问题是我需要在没有每年硬编码的情况下执行此操作,查询应该适用于任何数据集

提前致谢!

1 个答案:

答案 0 :(得分:3)

假设我们有原始[SalesData]

SalesYear   SalesMonth  Region  SalesTotal
---------   ----------  ------  ----------
2010        11          East    45
2010        11          West    58
2010        12          East    55
2010        12          West    63
2011        1           East    51
2011        1           West    54
2011        2           East    55
2011        2           West    61

我们可以创建一个[SalesTotals]查询,将月销售总额与年度总数相结合......

SELECT SalesYear & "-" & Format(SalesMonth, "00") AS SalesPeriod, Region, SalesTotal FROM SalesData
UNION ALL
SELECT SalesYear & "-Total", Region, SUM(SalesTotal) FROM SalesData GROUP BY SalesYear, Region;

......产生

SalesPeriod Region  SalesTotal
----------- ------  ----------
2010-11     East    45
2010-11     West    58
2010-12     East    55
2010-12     West    63
2011-01     East    51
2011-01     West    54
2011-02     East    55
2011-02     West    61
2010-Total  East    100
2010-Total  West    121
2011-Total  East    106
2011-Total  West    115

然后我们可以在[SalesTotals]查询中进行交叉表查询...

TRANSFORM Sum(SalesTotals.[SalesTotal]) AS SumOfSalesTotal
SELECT SalesTotals.[Region]
FROM SalesTotals
GROUP BY SalesTotals.[Region]
PIVOT SalesTotals.[SalesPeriod];

......产生

Region  2010-11 2010-12 2010-Total  2011-01  2011-02  2011-Total
------  ------- ------- ----------  -------  -------  ----------
East    45      55      100         51       55       106
West    58      63      121         54       61       115