当前位置: 移动技术网 > IT编程>数据库>MSSQL > 一个简单的交叉报表

一个简单的交叉报表

2020年04月15日  | 移动技术网IT编程  | 我要评论

恶搞石家庄,龙门山好玩吗,在线日剧

--行转列小实例
--创建测试表
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 ;

交叉前

 

 

交叉后

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网