2012-10-22 147 views
4

我遇到了一個我無法解決的問題,並且已經嘗試了一切。希望我能在這裏得到答案。在SQL Server 2008 R2中使用PIVOT表

我在我的表中的數據如下:

Id Month Val1 Val2 Val3 
1 Jan 70 80 90 
2 Jan 12 13 15 
3 Feb 12 67 99 
4 March 14 15 17.1 

從下面的結構,我想轉動基於val1列中的數據,所以我的輸出如下:

Jan Jan Feb March 
70 12 12 14 

回答

2

你會需要使用動態SQL我會想到(除非你的列將保持靜態)

需要執行的查詢是:

SELECT [Jan] = [Jan1], 
     [Jan] = [Jan2], 
     [Feb] = [Feb1], 
     [March] = [March1] 
FROM ( SELECT [PivotColumn] = [Month] + CONVERT(VARCHAR(10), ROW_NUMBER() OVER(PARTITION BY [Month] ORDER BY ID)), 
        Val1 
      FROM T 
     ) t 
     PIVOT 
     ( MAX(Val1) 
      FOR [PivotColumn] IN ([Jan1], [Jan2], [Feb1], [March1]) 
     ) pvt 

雖然我不知道爲什麼它應該是

Jan Jan Feb March 
70 12 12 14 

,而不是

Jan Jan Feb March 
12 70 12 14 

所以你可能需要在ROW_NUMBER功能與ORDER BY鼓搗。

,並建立這個動態的,你可以使用:

-- CREATE SAMPLE TABLE AND INSERT DATA 
CREATE TABLE #T (ID INT, Month VARCHAR(10), Val1 INT, Val2 INT, Val3 DECIMAL(5, 1)); 
INSERT #T VALUES (1, 'Jan', 70, 80, 90), (2, 'Jan', 12, 13, 15), (3, 'Feb', 12, 67, 99), (4, 'March', 14, 15, 17.1); 

-- DECLARE VARIABLES TO STORE THE COLUMN NAMES 
DECLARE @PivotList NVARCHAR(MAX) = '', 
     @ColumnList NVARCHAR(MAX) = ''; 

-- HERE USE ROW_NUMBER() TO UNIQUELY IDENTIFY VALUES FOR MONTHS 
-- THIS MEANS JAN: 70 AND JAN: 12 CAN BE IDENTIFIED SEPARATELY LATER, BUT RETAIN THE DUPLICATE COLUMN NAME [Jan] 
SELECT @ColumnList = @ColumnList + ', ' + QUOTENAME([Month]) + ' = ' + QUOTENAME([Month] + CONVERT(VARCHAR(10), ROW_NUMBER() OVER(PARTITION BY [Month] ORDER BY ID))), 
     @PivotList = @PivotList + ', ' + QUOTENAME([Month] + CONVERT(VARCHAR(10), ROW_NUMBER() OVER(PARTITION BY [Month] ORDER BY ID))) 
FROM #T 
ORDER BY ID; 

DECLARE @SQL NVARCHAR(MAX) = 'SELECT ' + STUFF(@ColumnList, 1, 2, '') + ' 
          FROM ( SELECT [PivotColumn] = [Month] + CONVERT(VARCHAR(10), ROW_NUMBER() OVER(PARTITION BY [Month] ORDER BY ID)), 
               Val1 
             FROM #T 
            ) t 
            PIVOT 
            ( MAX(Val1) 
             FOR [PivotColumn] IN (' + STUFF(@PivotList, 1, 2, '') + ') 
            ) pvt'; 

EXECUTE SP_EXECUTESQL @SQL; 

DROP TABLE #T; 

再次任何在ROW_NUMBER變化將需要也可以在生成列名和樞軸表中查詢ORDER BY子句中反映:

SELECT @ColumnList = @ColumnList + ', ' + QUOTENAME([Month]) + ' = ' + QUOTENAME([Month] + CONVERT(VARCHAR(10), ROW_NUMBER() OVER(PARTITION BY [Month] ORDER BY ID))), 
     @PivotList = @PivotList + ', ' + QUOTENAME([Month] + CONVERT(VARCHAR(10), ROW_NUMBER() OVER(PARTITION BY [Month] ORDER BY ID))) 
FROM #T 
ORDER BY ID; 
0

這個工作對我來說:

SELECT * FROM 
(select COUNT(isnull(intFileStatus,0)) as FileCount , case cast(intfilestatus as nvarchar(25)) when '0' then 'Not Allocated' when '1' then 'Assigned' when '2' then 'Pending' when '3' then 'Send For Qc' when '4' then 'Allocated To Qc' when '5' then 'Finish Qc' when '7' then 'Delivered' else cast(isnull(intFileStatus,0) as nvarchar (25)) end FileStatusfrom dbo.tblLPO_FileDetails group by intFileStatus) AS original 
PIVOT 
(
    MIN(FileCount) FOR [FileStatus] IN ([Not Allocated], [Assigned], [Pending],[Send For Qc],[Allocated To Qc],[Finish Qc],[Delivered]) 
) AS PivotTable 

試試看..