2016-07-15 33 views
0

我附上了一个屏幕快照。我提到了输入和输出。我需要一个SQL Server 2008/2012查询来获取输出。SQL SERVER 2008 QUERY将行转换为多列

Example input and output

+1

* T-SQL的动态支点*是你需要搜索的内容。 –

+0

请检查此:http://stackoverflow.com/questions/15745042/efficiently-convert-rows-to-columns-in-sql-server – TheGameiswar

+0

直接在帖子中添加图像,而不是作为链接 – Dave

回答

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等的coulmn顺序。有些事情如下。

结果

+------------+--------------+------------+--------------+------------+--------------+ 
| 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   |  
+------------+------------+------------+--------------+--------------+--------------+ 
+0

您的回答是正确的。 – Raj

+0

我问了另一个问题。 http://stackoverflow.com/questions/38449739/sql-server-2008-query-to-pick-top-distinct-3-dynamically?noredirect=1&lq=1 – Raj

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