Sqlserver行转列pivot()

Posted miaomiaotiaopi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sqlserver行转列pivot()相关的知识,希望对你有一定的参考价值。

PIVOT概述:

PIVOT用于将列值旋转为列名(即行转列),并在必要时对最终输出中所需的任何其余列值执行聚合。

PIVOT的一般语法:
SELECT [新表字段1,2,3…] FROM [原表名]
AS [原表别名]
PIVOT( [聚合函数] ( [原表字段1] ) FOR [原表字段2] IN ( [原表2值1],[原表字段2值2]… ) ) AS [新表别名]

语法解释:
1、PIVOT必须列举[原表字段2的值],列举的值必须用中括号 [ ] 包含起来,就算是字符串类型也不需要单引号 ’ ’
2、PIVOT中列举的值将作为新表的字段名称
3、为什么会有聚合函数?此处并没有GROUP BY 呀!偷偷告诉你,GROUP BY 是隐藏的,除了语句中出现的两个 [原表字段],其他[原表字段]将被GROUP BY,这样才使得上面的PIVOT结果出现多行
4、列举字段的这个组在原表中没有数据将以NULL值存在于PIVOT后的新表
5、PIVOT 和 UNPIVOT 关系运算符将表值表达式更改为pivot后的新表

CREATE TABLE [dbo].[StuInfo](
	[studentname] [varchar](30) NULL,
	[subject] [varchar](10) NULL,
	[grade] [int] NULL
) ON [PRIMARY]
//插入数据
INSERT INTO [dbo].[StuInfo]([studentname], [subject], [grade]) VALUES ('刘备', '语文', 80);
INSERT INTO [dbo].[StuInfo]([studentname], [subject], [grade]) VALUES ('刘备', '数学', 82);
INSERT INTO [dbo].[StuInfo]([studentname], [subject], [grade]) VALUES ('刘备', '英语', 84);
INSERT INTO [dbo].[StuInfo]([studentname], [subject], [grade]) VALUES ('赵云', '语文', 70);
INSERT INTO [dbo].[StuInfo]([studentname], [subject], [grade]) VALUES ('赵云', '数学', 74);
INSERT INTO [dbo].[StuInfo]([studentname], [subject], [grade]) VALUES ('赵云', '英语', 76);
INSERT INTO [dbo].[StuInfo]([studentname], [subject], [grade]) VALUES ('关羽', '语文', 90);
INSERT INTO [dbo].[StuInfo]([studentname], [subject], [grade]) VALUES ('关羽', '数学', 93);
INSERT INTO [dbo].[StuInfo]([studentname], [subject], [grade]) VALUES ('关羽', '英语', 94);
select * from  StuInfo


需求:按学生名称查询各科成绩:

静态case when 实现

select studentname,
	sum(case when subject='语文' then grade else NULL end) as 语文,
	sum(case when subject='数学' then grade else NULL end) as 数学,
	sum(case when subject='英语' then grade else NULL end) as 英语
from stuinfo
group by studentname

结果如下:

静态pivot

select * from  stuinfo pivot(sum(grade) for subject in ([语文],[数学],[英语]) ) as P 


如果每位同学都增加了物理成绩,静态语句需要做调整,这个时候可以用动态查询。

insert into StuInfo values('关羽','物理',100);
insert into StuInfo values('刘备','物理',90);
insert into StuInfo values('赵云','物理',80);

–动态case when

declare @SQL nvarchar(max)
declare @column nvarchar(max)

set @column=N'';

with sub as
(
select distinct subject  from stuinfo
)
select @column+=N'sum(case when subject='''+ subject + N''' then grade else null end )as '+ subject + N','
from sub 
select @column=SUBSTRING(@column,1,len(@column)-1)
select @SQL=N'select studentname,'+@column+N' from stuinfo
group by stuinfo.studentname'
print(@sql)
exec(@SQL)

–动态pivot

declare @sql nvarchar(max)
declare @column nvarchar(max)

set @column=N'';

with sub AS
(
select distinct subject
from stuinfo
)

select @column+=N'[' + cast(subject as varchar(30)) + N'],'
from sub  

select @column=SUBSTRING(@column,1,len(@column)-1)
select @sql=N'select pivot_stuinfo.studentname, ' + @column + 
N' from stuinfo pivot(sum(grade) for subject in (' + @column + N')) as pivot_stuinfo'

print(@sql)
exec (@sql)

结果为:

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)

 

以上是关于Sqlserver行转列pivot()的主要内容,如果未能解决你的问题,请参考以下文章

SqlServer PIVOT函数快速实现行转列,UNPIVOT实现列转行

Sqlserver中PIVOT行转列透视操作

SqlServer行转列(PIVOT),列转行(UNPIVOT)总结

死磕:SQL行转列汇总(全网最全最详细)

SqlServer基本操作

MySQL---行转列