一个简单的交叉报表
Posted 好记性不如烂笔头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的交叉报表相关的知识,希望对你有一定的参考价值。
--行转列小实例 --创建测试表 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 ;
以上是关于一个简单的交叉报表的主要内容,如果未能解决你的问题,请参考以下文章