在访问中将行转换为列

时间:2017-11-14 17:46:21

标签: sql ms-access

我有以下格式的数据集:

 ID   A  B  C  D 
 1    x  x  x  x
 2    x  x  x  x 
 3    x  x  x  x
 4    x  x  x  x

我想将其转换为ID与行的值重复的格式,例如看起来像下面这样:

ID    Letter    Value
1       A         x
1       B         x
1       C         x
1       D         x
2       A         x
2       B         x 
2       C         x
2       D         X

依旧......

非常感谢提前。

2 个答案:

答案 0 :(得分:0)

有用的链接:

Columns to Rows in MS Access

答案 1 :(得分:0)

UNION语句允许您这样做:

SELECT * FROM (
    SELECT [ID], "A" As [Letter], [A] As [Value] FROM [yourTable]
    UNION ALL
    SELECT [ID], "B" As [Letter], [B] As [Value] FROM [yourTable]
    UNION ALL
    SELECT [ID], "C" As [Letter], [C] As [Value] FROM [yourTable]
    UNION ALL
    SELECT [ID], "D" As [Letter], [D] As [Value] FROM [yourTable]
) ORDER BY [ID], [Letter]