--1 首先我们先创建一个测试表,往里面插入测试数据,返回表记录如图2所示: --创建测试表 CREATE TABLE [dbo].[TestRows2Columns]( [Id] [int] IDENTITY(1,1) NOT NULL, [UserName] [nvarchar](50) NULL, [Subject] [nvarchar](50) NULL, [Source] [numeric](18,0) NULL ) 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 GO SELECT * FROM [TestRows2Columns]
--2 先以静态的方式实现行转列,效果如图3所示: --静态sql行转列 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] GO
--3 接着以动态的方式实现行转列,这是使用拼接SQL的方式实现的,所以它适用于SQL Server 2000以上的数据库版本,执行脚本返回的结果如图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)
--4 在SQL Server 2005之后有了一个专门的PIVOT 和 UNPIVOT 关系运算符做行列之间的转换,下面是静态的方式实现的,实现效果如图4所示: --静态PIVOT行转列 SELECT * FROM ( SELECT [UserName], [Subject], [Source] FROM TestRows2Columns ) p PIVOT (SUM([Source]) FOR [Subject] IN ([数学],[英语],[语文])) AS pvt ORDER BY pvt.[UserName]; GO
--5 把上面静态的SQL基础上进行修改,这样就不用理会记录里面存储了什么,需要转成什么列名的问题了,脚本如下,效果如图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)
--6 需要不断的修改成他自己环境中表名、分组列、行转列字段、字段值这几个参数,逻辑如图5所示,所以,我继续对上面的脚本进行修改,你只要设置自己的参数就可以实现行转列了,效果如图4所示: --带条件查询的参数化动态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)