恶搞石家庄,龙门山好玩吗,在线日剧
--行转列小实例 --创建测试表 if object_id(n'test', n'u') is not null drop table test go with pivottable as ( select 'xxx' as czy, '点赞' as czlx, 2 as num union all select 'xxx', '浏览' as czlx, 14 as num union all select 'yyy', '浏览' as czlx, 10 as num union all select 'zzz', '浏览', 30 union all select 'zzz', '点赞', 3 ) select * into test from pivottable go --创建存储过程 if exists(select name from sysobjects where name = 'usp_getpivotinfo') drop proc usp_getpivotinfo go create proc usp_getpivotinfo as declare @czlx varchar(500), @sql varchar(2000) select @czlx = stuff((select distinct ',[' + czlx + ']' from test for xml path ('')),1,1,'') --select @czlx set @sql = 'select czy, {#} from test pivot(sum(num) for czlx in ({#})) as t'; set @sql = replace(@sql, '{#}', @czlx); exec(@sql); go exec usp_getpivotinfo ;
交叉前
交叉后
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
数据库SQL---数据库、基本表、视图、索引的定义、修改、删除
在 Azure CentOS VM 中配置 SQL Server 2019 AG - (上)
在 Azure CentOS VM 中配置 SQL Server 2019 AG - (下)
网友评论