Sqlserver中PIVOT行转列透视操作
创建表:
IF OBJECT_ID('T040_PRODUCT_SALES') IS NOT NULL DROP TABLE T040_PRODUCT_SALES create table T040_PRODUCT_SALES ( ID INT IDENTITY(1,1), ProductName VARCHAR(20), SaleMonth INT, SalesCount INT )
插入数据并排序:
INSERT INTO T040_PRODUCT_SALES VALUES ('Bicycle',1,1), ('Shoes',2,2), ('Clothes',3,3), ('Books',4,4), ('Medicine',5,5), ('Drinks',6,6), ('Shoes',7,7), ('Books',1,2), ('Bicycle',1,3), ('Medicine',1,4), ('Clothes',1,5), ('Mobile Phone',1,6), ('Books',1,7), ('Medicine',1,8), ('Shoes',1,9), ('Bicycle',2,10) SELECT ProductName, SaleMonth, SUM(SalesCount) AS SalesCount FROM T040_PRODUCT_SALES GROUP BY ProductName, SaleMonth ORDER BY ProductName, SaleMonth
格式:
/**** SELECT 非透视列, [透视列 1] AS '列名1', [透视列 2] AS '列名2', [透视列 3] AS '列名3' FROM ( -- 源数据 SELECT 非透视列, 透视列值的来源列, 需要聚合的值 FROM 表 )AS 别名 PIVOT ( SUM(需要聚合的值) FOR 透视列值的来源列 IN ([透视列 1],[透视列 2],[透视列 3]) )AS 别名 ****/
行转列的代码:
select ProductName, ISNULL([1],0) AS '1', ISNULL([2],0) AS '2', ISNULL([3],0) AS '3', ISNULL([4],0) AS '4', ISNULL([5],0) AS '5', ISNULL([6],0) AS '6' from ( select ProductName, SaleMonth, SalesCount from T040_PRODUCT_SALES) as Sales pivot ( SUM(SalesCount) FOR SaleMonth IN([1],[2],[3],[4],[5],[6]) ) as PIVOTBL
结果:
通过其他方式实现:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TestRows2Columns]') AND type in (N'U')) DROP TABLE [dbo].[TestRows2Columns] GO CREATE TABLE [dbo].[TestRows2Columns]( [Id] [int] IDENTITY(1,1) NOT NULL, [UserName] [nvarchar](50) NULL, [Subject] [nvarchar](50) NULL, [Source] [numeric](18, 0) NULL ) ON [PRIMARY] GO INSERT INTO [TestRows2Columns] ([UserName],[Subject],[Source]) SELECT N'张三',N'语文',60 UNION ALL SELECT N'李四',N'数学',70 UNION ALL SELECT N'王五',N'英语',80 UNION ALL SELECT N'王五',N'数学',75 UNION ALL SELECT N'王五',N'语文',57 UNION ALL SELECT N'李四',N'语文',80 UNION ALL SELECT N'张三',N'英语',100
实现方式:
--1:静态拼接行转列 SELECT [UserName], SUM(CASE [Subject] WHEN '数学' THEN [Source] ELSE 0 END) AS '[数学]', SUM(CASE [Subject] WHEN '英语' THEN [Source] ELSE 0 END) AS '[英语]', SUM(CASE [Subject] WHEN '语文' THEN [Source] ELSE 0 END) AS '[语文]' FROM [TestRows2Columns] GROUP BY [UserName]
--2.动态拼接 DECLARE @sql VARCHAR(8000) SET @sql='SELECT [UserName],' --它只用于给输入的字符串加一对方括号,并返回新形成的字符串 SELECT @sql=@sql+ 'SUM(CASE [Subject] WHEN '''+[Subject]+''' THEN [Source] ELSE 0 END) AS '''+QUOTENAME([Subject])+''',' FROM (SELECT DISTINCT [Subject] FROM [TestRows2Columns]) AS a SELECT @sql = LEFT(@sql,LEN(@sql)-1) + ' FROM [TestRows2Columns] GROUP BY [UserName]' PRINT(@sql) EXEC(@sql)
--3:静态PIVOT行转列 SELECT * FROM ( SELECT [UserName] , [Subject] , [Source] FROM [TestRows2Columns] ) p PIVOT ( SUM([Source]) FOR [Subject] IN ( [数学],[英语],[语文] ) ) AS pvt ORDER BY pvt.[UserName]; --4:动态PIVOT行转列 DECLARE @sql_str VARCHAR(8000) DECLARE @sql_col VARCHAR(8000) SELECT @sql_col = ISNULL(@sql_col + ',','') + QUOTENAME([Subject]) FROM [TestRows2Columns] GROUP BY [Subject] SET @sql_str = ' SELECT * FROM ( SELECT [UserName],[Subject],[Source] FROM [TestRows2Columns]) p PIVOT (SUM([Source]) FOR [Subject] IN ( '+ @sql_col +') ) AS pvt ORDER BY pvt.[UserName]' PRINT (@sql_str) EXEC (@sql_str)
--5.参数化 DECLARE @sql_str NVARCHAR(MAX) DECLARE @sql_col NVARCHAR(MAX) DECLARE @tableName SYSNAME --行转列表 DECLARE @groupColumn SYSNAME --分组字段 DECLARE @row2column SYSNAME --行变列的字段 DECLARE @row2columnValue SYSNAME --行变列值的字段 SET @tableName = 'TestRows2Columns' SET @groupColumn = 'UserName' SET @row2column = 'Subject' SET @row2columnValue = 'Source' --从行数据中获取可能存在的列 SET @sql_str = N' SELECT @sql_col_out = ISNULL(@sql_col_out + '','','''') + QUOTENAME(['+@row2column+']) FROM ['+@tableName+'] GROUP BY ['+@row2column+']' --PRINT @sql_str EXEC sp_executesql @sql_str,N'@sql_col_out NVARCHAR(MAX) OUTPUT',@sql_col_out=@sql_col OUTPUT --PRINT @sql_col SET @sql_str = N' SELECT * FROM ( SELECT ['+@groupColumn+'],['+@row2column+'],['+@row2columnValue+'] FROM ['+@tableName+']) p PIVOT (SUM(['+@row2columnValue+']) FOR ['+@row2column+'] IN ( '+ @sql_col +') ) AS pvt ORDER BY pvt.['+@groupColumn+']' --PRINT (@sql_str) EXEC (@sql_str)
--6:带条件查询的参数化动态PIVOT行转列 DECLARE @sql_str NVARCHAR(MAX) DECLARE @sql_col NVARCHAR(MAX) DECLARE @sql_where NVARCHAR(MAX) DECLARE @tableName SYSNAME --行转列表 DECLARE @groupColumn SYSNAME --分组字段 DECLARE @row2column SYSNAME --行变列的字段 DECLARE @row2columnValue SYSNAME --行变列值的字段 SET @tableName = 'TestRows2Columns' SET @groupColumn = 'UserName' SET @row2column = 'Subject' SET @row2columnValue = 'Source' SET @sql_where = 'WHERE UserName = ''王五''' --从行数据中获取可能存在的列 SET @sql_str = N' SELECT @sql_col_out = ISNULL(@sql_col_out + '','','''') + QUOTENAME(['+@row2column+']) FROM ['+@tableName+'] '+@sql_where+' GROUP BY ['+@row2column+']' --PRINT @sql_str EXEC sp_executesql @sql_str,N'@sql_col_out NVARCHAR(MAX) OUTPUT',@sql_col_out=@sql_col OUTPUT --PRINT @sql_col SET @sql_str = N' SELECT * FROM ( SELECT ['+@groupColumn+'],['+@row2column+'],['+@row2columnValue+'] FROM ['+@tableName+']'+@sql_where+') p PIVOT (SUM(['+@row2columnValue+']) FOR ['+@row2column+'] IN ( '+ @sql_col +') ) AS pvt ORDER BY pvt.['+@groupColumn+']' --PRINT (@sql_str) EXEC (@sql_str)