1

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

作为查询的结果,我想看到的是:

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

如何为年度创建这些小计?(没关系,如果年份数据将在所有月份之后结束)问题是我需要每年都这样做而不需要硬编码,查询应该适用于任何数据集

提前致谢!

4

1 回答 1

2

假设我们有原始 [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
于 2013-04-25T09:06:54.233 回答