不确定是否可能,但我正在使用Access 2010并拥有下表
> Reference|| Start Date || Month 1 || Month 2 || Month 3 || month N
> 123 || 01/02/2012 || 50 || 50 || 70 || 50 321
> 321 || 12/05/2012 || 60 || 40 || 90 || 0
我想要实现的是将其转换为
表Reference || Start date || January 12 || February 12 || March 12 || April 12 || May 12 ||..
123 || 01/02/2012 || 0 || 50 || 50 || 70 || 50 ||
321 || 12/05/2012 || 0 || 0 || 0 || 0 || 60 ||
这是否可以使用Access,因为第1个月与1月12日无关,尽管它可以从Start DAate派生
问候 Maudise
答案 0 :(得分:1)
从UNION查询开始,以“解锁”您的数据:
SELECT Reference, [Start Date], 0 AS [Month Offset], [Month 1] AS Qty
FROM TableN
UNION ALL
SELECT Reference, [Start Date], 1 AS [Month Offset], [Month 2] AS Qty
FROM TableN
UNION ALL
SELECT Reference, [Start Date], 2 AS [Month Offset], [Month 3] AS Qty
FROM TableN
UNION ALL
SELECT Reference, [Start Date], 3 AS [Month Offset], [Month 4] AS Qty
FROM TableN
重复UNION ALL模式,直到包含原始表中的所有[Month N]字段。将此查询保存为[QtyByMonth]。结果应如下所示
Reference Start Date Month Offset Qty
--------- ---------- ------------ ---
123 2012-02-01 0 50
321 2012-05-12 0 60
123 2012-02-01 1 50
321 2012-05-12 1 40
123 2012-02-01 2 70
321 2012-05-12 2 90
123 2012-02-01 3 50
321 2012-05-12 3 0
现在创建一个查询,创建将按月执行交叉表的基础:
SELECT
Reference,
[Start Date],
DateAdd("m", [Month Offset], DateSerial(Year([Start Date]), Month([Start Date]), 1)) AS [FirstOfMonth],
Qty
FROM QtyByMonth
...返回...
Reference Start Date FirstOfMonth Qty
--------- ---------- ------------ ---
123 2012-02-01 2012-02-01 50
321 2012-05-12 2012-05-01 60
123 2012-02-01 2012-03-01 50
321 2012-05-12 2012-06-01 40
123 2012-02-01 2012-04-01 70
321 2012-05-12 2012-07-01 90
123 2012-02-01 2012-05-01 50
321 2012-05-12 2012-08-01 0
将该查询保存为[BasisForXtab]。现在基于该构建交叉表......
TRANSFORM Sum(BasisForXtab.Qty) AS SumOfQty
SELECT BasisForXtab.Reference, BasisForXtab.[Start Date]
FROM BasisForXtab
GROUP BY BasisForXtab.Reference, BasisForXtab.[Start Date]
PIVOT Format([FirstOfMonth],"yyyy-MM");
...返回...
Reference Start Date 2012-02 2012-03 2012-04 2012-05 2012-06 2012-07 2012-08
--------- ---------- ------- ------- ------- ------- ------- ------- -------
123 2012-02-01 50 50 70 50
321 2012-05-12 60 40 90 0