我有一个奇怪的访问数据库表,我需要在使用Tableau之前进行数据透视。我使用Access 2013.数据完全是假的,但结构是正确的。
共有200个文件,每个文件大约1500次运行。我有50年的指标。我每个文件/运行大约有10个指标。
+--------------------------------------------------------+
| DataTable |
+-------------+--------------+------+------+------+------+
| FileIDRunID | Metric | 1999 | 2000 | 2001 | 2002 |
+-------------+--------------+------+------+------+------+
| 00000100001 | Breakfast | 45 | 47 | 48 | 49 |
| 00000100001 | Lunch | 27 | 37 | 50 | 99 |
| 00000100002 | Breakfast | 45 | 47 | 48 | 49 |
| 00000100002 | Lunch | 27 | 37 | 50 | 99 |
| 00000200001 | Breakfast | 45 | 47 | 48 | 49 |
| 00000200001 | Lunch | 27 | 37 | 50 | 99 |
| 00000200002 | Breakfast | 45 | 47 | 48 | 49 |
| 00000200002 | Lunch | 27 | 37 | 50 | 99 |
+-------------+--------------+------+------+------+------+
我想转动并将指标作为列,将年份作为单独的行。
+------------------------------------------------+
| DataTableView |
+-------------+--------------+-----------+-------+
| FileIDRunID | Year | Breakfast | Lunch |
+-------------+--------------+-----------+-------+
| 00000100001 | 1999 | 45 | 47 |
| 00000100001 | 2000 | 27 | 37 |
| 00000100002 | 1999 | 45 | 47 |
| 00000100002 | 2000 | 27 | 37 |
| 00000200001 | 1999 | 45 | 47 |
| 00000200001 | 2000 | 27 | 37 |
| 00000200002 | 1999 | 45 | 47 |
| 00000200002 | 2000 | 27 | 37 |
+-------------+--------------+-----------+-------+
我设法调整了一年。
TRANSFORM FIRST(DataTable.[1999])
SELECT FileIDRunID, '1999' as Year
FROM DataTable
GROUP BY FileIDRunID
PIVOT DataTable.Metric
我认为下一步是每年手动使用UNION ALL。当我在第二年尝试时,虽然我收到错误。
TRANSFORM FIRST(DataTable.[1999])
SELECT FileIDRunID, '1999' as Year
FROM DataTable
GROUP BY FileIDRunID
PIVOT DataTable.Metric
UNION ALL
TRANSFORM FIRST(DataTable.[2000])
SELECT FileIDRunID, '2000' as Year
FROM DataTable
GROUP BY FileIDRunID
PIVOT DataTable.Metric
查询表达式' DataTable.Metric中的语法错误(缺少运算符) UNION ALL TRANSFORM FIRST(DataTable。[2000]) 选择FileID,RunID,' 2000'作为一年 来自DataTable GROUP BY FileIDRunID PIVOT DataTable.Metric'
我希望这里有人有更好的主意:)。
答案 0 :(得分:2)
TRANSFORM
可以only occur at the start的SQL语句。
首先对所有年份进行拆解,然后才转动指标。现在,显然是MS Access 在涉及复杂查询(许多工会)时有其局限性,最好通过 中间表:
SELECT FileIDRunID, Metric, Year, Value
FROM (
SELECT FileIDRunID, Metric, 1999 As Year, [1999] As Value
FROM DataTable
UNION ALL
SELECT FileIDRunID, Metric, 2000 As Year, [2000] As Value
FROM DataTable
UNION ALL
SELECT FileIDRunID, Metric, 2001 As Year, [2001] As Value
FROM DataTable
UNION ALL
SELECT FileIDRunID, Metric, 2002 As Year, [2002] As Value
FROM DataTable
)
INTO myTempTable;
我有union
- 4年了,但你应该检查MS Access拒绝该语句过于复杂的程度。
然后,添加更多年份,使用相同数量的union
s:
INSERT INTO myTempTable (FileIDRunID, Metric, Year, Value)
SELECT FileIDRunID, Metric, Year, Value
FROM (
SELECT FileIDRunID, Metric, 2003 As Year, [2003] As Value
FROM DataTable
UNION ALL
SELECT FileIDRunID, Metric, 2004 As Year, [2004] As Value
FROM DataTable
UNION ALL
SELECT FileIDRunID, Metric, 2005 As Year, [2005] As Value
FROM DataTable
UNION ALL
SELECT FileIDRunID, Metric, 2006 As Year, [2006] As Value
FROM DataTable
);
等...最后这样做:
TRANSFORM FIRST(Value)
SELECT FileIDRunID,
Year
FROM myTempTable
GROUP BY FileIDRunID,
Year
PIVOT Metric
然后删除临时表并压缩数据库以恢复原始大小。如果可能,重新设计数据库以使用临时表中完全不透明的结构。
答案 1 :(得分:0)
想想我可能已经找到了一个'回答。仍然希望有更优雅的东西。
首先,将单个列转换为单个列,并将其转换为名为" DataTableFormatted"
的新视图SELECT FileIDRunID, Metric, '1999' as Year
FROM DataTable
UNION ALL
SELECT FileIDRunID, Metric, '2000' as Year
FROM DataTable
然后,在该表上执行转换。
TRANSFORM FIRST(DataValue)
SELECT FileIDRunID, Year
FROM DataTableFormatted
GROUP BY FileIDRunID, Year
PIVOT DataTableFormatted.Metric