如何在Access中模拟UNPIVOT?

发布于 2024-12-02 06:54:40 字数 567 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女皇必胜 2024-12-09 06:54:40

此查询...

SELECT ID, A, B, C, [Key 1] AS key_field
FROM tblUnpivotSource
UNION ALL
SELECT ID, A, B, C, [Key 2] AS key_field
FROM tblUnpivotSource
UNION ALL
SELECT ID, A, B, C, [Key 3] AS key_field
FROM tblUnpivotSource;

...返回此记录集(使用示例表值作为tblUnpivotSource)...

ID A B C key_field
-- - - - ---------
 1 x y z         3
 2 x y z        57
 1 x y z       199
 2 x y z       234
 1 x y z       452
 2 x y z       452

This query ...

SELECT ID, A, B, C, [Key 1] AS key_field
FROM tblUnpivotSource
UNION ALL
SELECT ID, A, B, C, [Key 2] AS key_field
FROM tblUnpivotSource
UNION ALL
SELECT ID, A, B, C, [Key 3] AS key_field
FROM tblUnpivotSource;

... returns this recordset (using your sample table values as tblUnpivotSource) ...

ID A B C key_field
-- - - - ---------
 1 x y z         3
 2 x y z        57
 1 x y z       199
 2 x y z       234
 1 x y z       452
 2 x y z       452
伴我心暖 2024-12-09 06:54:40
  1. 您可以创建一个辅助表,将所有列名作为值
    (可以使用excel将表格的第一行复制到excel>粘贴特殊>转置)

  2. 在表格中创建自动增量列并索引该列

  3. 创建如下所示的新交叉联接查询

SELECT ID, A, B, C
       , AUX_TABLE.KEY_FIELD
       , DLookUp("[" & [AUX_TABLE].[KEY_FIELD] & "]","TABLE","[ID] = " & [TABLE].[ID]) AS KEY_VALUE
FROM TABLE, AUX_TABLE;

缺点是您必须维护 AUX_TABLE 才能保持其正常工作。但如果这是一次性的事情,这可能是正确的选择。

  1. 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)

  2. Create in your table a auto increment column and index this column

  3. Create a new cross join query like the following

SELECT ID, A, B, C
       , AUX_TABLE.KEY_FIELD
       , DLookUp("[" & [AUX_TABLE].[KEY_FIELD] & "]","TABLE","[ID] = " & [TABLE].[ID]) AS KEY_VALUE
FROM TABLE, AUX_TABLE;

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.

剪不断理还乱 2024-12-09 06:54:40

不幸的是,没有简单的方法可以通过访问来做到这一点。您可以通过使用 UNION 获取每个值来做到这一点

SELECT ID, A, B, C, [Key 1] As key
FROM Table
WHERE [Key 1] = 3

UNION ALL

SELECT ID, A, B, C, [Key 1] As key
FROM Table
WHERE [Key 1] = 57

UNION ALL

SELECT ID, A, B, C, [Key 2] As key
FROM Table
WHERE [Key 2] = 199

UNION ALL

SELECT ID, A, B, C, [Key 2] As key
FROM Table
WHERE [Key 2] = 234

UNION ALL

SELECT ID, A, B, C, [Key 3] As key
FROM Table
WHERE [Key 3] = 452

Unfortunately there is no easy way to do this with access. You can do this by using a UNION to get each value

SELECT ID, A, B, C, [Key 1] As key
FROM Table
WHERE [Key 1] = 3

UNION ALL

SELECT ID, A, B, C, [Key 1] As key
FROM Table
WHERE [Key 1] = 57

UNION ALL

SELECT ID, A, B, C, [Key 2] As key
FROM Table
WHERE [Key 2] = 199

UNION ALL

SELECT ID, A, B, C, [Key 2] As key
FROM Table
WHERE [Key 2] = 234

UNION ALL

SELECT ID, A, B, C, [Key 3] As key
FROM Table
WHERE [Key 3] = 452
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文