Access将一行转换为多行

时间:2014-12-18 21:25:45

标签: sql select ms-access-2007 ms-access-2010

我有一个名为Student_Course的表格,如下所示:

 ID  | Name  |  Course 1 | Course 2  |  Course 3
 1   | A     |    90    |    50    |     60
 2   | B     |    80    |    70    |     30
 3   | C     |    70    |    60    |     20

我想做一些查询并将上表转换为以下格式:

 ID |   Name |  Course Name   |  Score
1   |    A   |   Course 1     |    90       
1   |    A   |   Course 2     |    50
1   |    A   |   Course 3     |    60
2   |    B   |   Course 1     |    80    
2   |    B   |   Course 2     |    70  
2   |    B   |   Course 3     |    30
3   |    C   |   Course 1     |    70     
3   |    C   |   Course 2     |    60        
3   |    C   |   Course 3     |    20

1 个答案:

答案 0 :(得分:1)

您可以使用几个union all运算符:

SELECT ID, Name, 'Course 1' AS [Course Name], Course1 AS Score
FROM   Student_Course
UNION ALL
SELECT ID, Name, 'Course 2' AS [Course Name], Course2 AS Score
FROM   Student_Course
UNION ALL
SELECT ID, Name, 'Course 3' AS [Course Name], Course3 AS Score
FROM   Student_Course