2016-07-15 53 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