SQL SERVER 2008 QUERY将行转换为多列

时间:2016-07-15 11:18:28

标签: sql-server

我附了一个屏幕截图。我已经提到了输入和所需的输出。我需要一个SQL server 2008/2012查询来获取输出。

Example input and output

2 个答案:

答案 0 :(得分:0)

你需要使用PIVOT。它应该像下面这样。如果您不知道PIVOT的工作原理,请尝试在线学习Excel Pivot教程,让自己更熟悉其逻辑。

WITH PivotData AS
(
    SELECT
        AssignmentName,
        StudentName,
        Grade
    FROM TableName
)

SELECT
    StudentName,
    Assignment1,
    Assignment2,
    Assignment3
FROM PivotData
PIVOT
(
    SUM(Grade)
    FOR AssignmentName
    IN (Assignment1, Assignment2, Assignment3)
) AS PivotResult
ORDER BY StudentName

PIVOT and UNPIVOT in T-SQL

Pivot in Excel

答案 1 :(得分:0)

您可以使用动态SQL查询。

<强>查询

declare @sql as varchar(max);

select @sql = 'select ' + stuff((
           select ', max(case StudentID when ' 
           + cast(t.StudentID as varchar(10))                               
           + ' then  StudentKey end) as StudentID' 
           + cast(t.StudentID as varchar(10))
           +', max(case StudentID when ' + cast(t.StudentID as varchar(10)) 
           + ' then  StudentName end) as StudentName' 
           + cast(t.StudentID as varchar(10)) 
           from (select distinct top 3 * from studentTable order by StudentID)t
           for xml path('')
        ), 1, 2, '') + ' from studentTable;';

exec(@sql);

这将以1 StudentId然后StudentName的顺序给出结果,依此类推。有些事情如下。

<强>结果

+------------+--------------+------------+--------------+------------+--------------+
| StudentID1 | StudentName1 | StudentID2 | StudentName2 | StudentID3 | StudentName3 |
+------------+--------------+------------+--------------+------------+--------------+
| 125        |     A        |  225       |     B        |  325       |      C       |
+------------+--------------+------------+--------------+------------+--------------+

如果你希望结果像所有studentId列那样先是studentName列。然后

<强>查询

declare @sql as varchar(max);

select @sql = 'select ' + stuff((
                    select ', max(case StudentID when ' 
                    + cast(t.StudentID as varchar(10)) 
                    + ' then  StudentKey end) as StudentID' 
                    + cast(t.StudentID as varchar(10))
                    from (select distinct top 3 * from studentTable order by StudentID)t
                    for xml path('')
                    ), 1, 2, '')
                    + ',' 
                    + stuff((
                    select ', max(case StudentID when ' 
                    + cast(t.StudentID as varchar(10)) 
                    + ' then  StudentName end) as StudentName' 
                    +  cast(t.StudentID as varchar(10)) 
                    from (select distinct top 3 * from studentTable order by StudentID)t
                    for xml path('')
                    ), 1, 2, '')
                    + ' from studentTable;';

exec(@sql);

<强>结果

+------------+------------+------------+--------------+--------------+--------------+
| StudentID1 | StudentID2 | StudentID3 | StudentName1 | StudentName2 | StudentName3 |
+------------+------------+------------+--------------+--------------+--------------+
| 125        |  225       |  325       | A            |   B          |  C           |     
+------------+------------+------------+--------------+--------------+--------------+