資料透視表與列串聯
問題:
問題:ID TYPE SUBTYPE COUNT MONTH 1 A Z 1 7/1/2008 1 A Z 3 7/1/2008 2 B C 2 7/2/2008 1 A Z 3 7/2/2008
ID A_Z B_C MONTH 1 4 0 7/1/2008 2 0 2 7/2/2008 1 0 3 7/2/2008
如何轉換資料庫表具有以下結構:
進入具有以下結構的表:DECLARE @sql AS varchar(max); SET @sql = ' ;WITH p AS ( SELECT ID, [MONTH], [TYPE] + ''_'' + SUBTYPE AS PIVOT_CODE, SUM(COUNT) AS [COUNT] FROM stackoverflow_159456 GROUP BY ID, [MONTH], [TYPE] + ''_'' + SUBTYPE ) SELECT ID, [MONTH], ' + @select_list + ' FROM p PIVOT ( SUM(COUNT) FOR PIVOT_CODE IN ( ' + @pivot_list + ' ) ) AS pvt ';
以上是如何在 SQL Server 中透視表並連接列?的詳細內容。更多資訊請關注PHP中文網其他相關文章!