如何在Access中模拟UNPIVOT?
UNPIVOT
在 MS SQL-Server 2005 中可用,但据我所知在 MS Access 2010 中不可用。如何通过板载方法实现它?比如我有一个表
ID | A | B | C | Key 1 | Key 2 | Key 3
---------------------------------------
1 | x | y | z | 3 | 199 | 452
2 | x | y | z | 57 | 234 | 452
,想要有一个像
ID | A | B | C | Key
--------------------
1 | x | y | z | 3
2 | x | y | z | 57
1 | x | y | z | 199
2 | x | y | z | 234
2 | x | y | z | 452
Key 452这样的表就是一个特例。目前我在 OLEDB/ATL C++ 中进行旋转。虽然速度足够快,但我仍然很好奇。 Access 2010 最有效的 SQL 语句是什么?
UNPIVOT
is available in MS SQL-Server 2005, but AFAIK not in MS Access 2010. How can it be implemented with on-board means? For example, I have a table
ID | A | B | C | Key 1 | Key 2 | Key 3
---------------------------------------
1 | x | y | z | 3 | 199 | 452
2 | x | y | z | 57 | 234 | 452
and want to have a table like
ID | A | B | C | Key
--------------------
1 | x | y | z | 3
2 | x | y | z | 57
1 | x | y | z | 199
2 | x | y | z | 234
2 | x | y | z | 452
Key 452 is a special case. Currently I do the rotation in OLEDB/ATL C++. Although it is fast enough I'm still curious. What is the most efficient SQL statement for Access 2010 here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此查询...
...返回此记录集(使用示例表值作为tblUnpivotSource)...
This query ...
... returns this recordset (using your sample table values as tblUnpivotSource) ...
您可以创建一个辅助表,将所有列名作为值
(可以使用excel将表格的第一行复制到excel>粘贴特殊>转置)
在表格中创建自动增量列并索引该列
创建如下所示的新交叉联接查询
缺点是您必须维护
AUX_TABLE
才能保持其正常工作。但如果这是一次性的事情,这可能是正确的选择。You can create a auxiliary table with all column names as values
(can use excel copy the first row of your table to excel > paste special > transpose)
Create in your table a auto increment column and index this column
Create a new cross join query like the following
Downside would be you have to maintain
AUX_TABLE
to keep that working. But if this is a one-time-thing this might be the way to go.不幸的是,没有简单的方法可以通过访问来做到这一点。您可以通过使用
UNION
获取每个值来做到这一点Unfortunately there is no easy way to do this with access. You can do this by using a
UNION
to get each value