我来一个,凑凑热闹
表T
id int ,showname varchar(50) ,value varchar(50)
数据:
1 name1 value1
2 name2 value2
3 name3 value3
4 name4 value4
语句:
declare @xc nvarchar(1024)
set @xc = ''
select @xc=@xc + (case [a].[column] when [a].[column] then '(select [value] from t where showname='''+[a].[column]+''') as '+[a].[column]+' ,' end) from ( select distinct showname as [column] from t) as a
set @xc = left(@xc,len(@xc) -1)
print @xc
set @xc = 'select ' + @xc
exec sp_executesql @xc
这是我的思路,具体写的时候,感觉到Sql的组合比较麻烦,能帮我写一下吗?
=======================================================================
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_qry]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_qry]
GO
--判断横向字段是否大于纵向字段数目,如果是,则交换纵横字段
set @s='declare @a sysname
if(select case when count(distinct ['+@纵轴+'])<count(distinct ['+@横轴+']) then 1 else 0 end
from ['+@TableName+'])=1
select @a=@纵轴,@纵轴=@横轴,@横轴=@a'
exec sp_executesql @s
,N'@纵轴 sysname out,@横轴 sysname out'
,@纵轴 out,@横轴 out
--生成交叉表处理语句
set @s='
set @s=''''
select @s=@s+'',[''+cast(['+@横轴+'] as varchar)+'']=sum(case ['+@横轴
+'] when ''''''+cast(['+@横轴+'] as varchar)+'''''' then ['+@表体内容+'] else 0 end)''
from ['+@TableName+']
group by ['+@横轴+']'
exec sp_executesql @s
,N'@s varchar(8000) out'
,@sql out
--是否生成合计字段的处理
declare @sum1 varchar(200),@sum2 varchar(200),@sum3 varchar(200)
select @sum1=case @是否加横向合计
when 1 then ',[合计]=sum(['+@表体内容+'])'
else '' end
,@sum2=case @是否家纵向合计
when 1 then '['+@纵轴+']=case grouping(['
+@纵轴+']) when 1 then ''合计'' else cast(['
+@纵轴+'] as varchar) end'
else '['+@纵轴+']' end
,@sum3=case @是否家纵向合计
when 1 then ' with rollup'
else '' end
--生成交叉表
exec('select '+@sum2+@sql+@sum1+'
from ['+@TableName+']
group by ['+@纵轴+']'+@sum3)
go
===========================================================================
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_qry]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_qry]
GO
/*--生成交叉表的简单通用存储过程
根据指定的表名,纵横字段,统计字段,自动生成交叉表
并可根据需要生成纵横两个方向的合计
注意,横向字段数目如果大于纵向字段数目,将自动交换纵横字段
如果不要此功能,则去掉交换处理部分
--邹建 204.06--*/
/*--调用示例
exec p_qry 'syscolumns','id','colid','colid','name like ''s%''',1,1
--*/
--规范条件
set @条件=case when @条件<>'' then ' where ('+@条件+')' else '' end
--判断横向字段是否大于纵向字段数目,如果是,则交换纵横字段
set @s='declare @a sysname
if(select case when count(distinct ['+@纵轴+'])<count(distinct ['+@横轴+']) then 1 else 0 end
from ['+@TableName+'] '+@条件+')=1
select @a=@纵轴,@纵轴=@横轴,@横轴=@a'
exec sp_executesql @s
,N'@纵轴 sysname out,@横轴 sysname out'
,@纵轴 out,@横轴 out
--生成交叉表处理语句
set @s='
set @s=''''
select @s=@s+'',[''+cast(['+@横轴+'] as varchar)+'']=sum(case ['+@横轴
+'] when ''''''+cast(['+@横轴+'] as varchar)+'''''' then ['+@表体内容+'] else 0 end)''
from ['+@TableName+']
'+@条件+'
group by ['+@横轴+']'
exec sp_executesql @s
,N'@s varchar(8000) out'
,@sql out
--是否生成合计字段的处理
declare @sum1 varchar(200),@sum2 varchar(200),@sum3 varchar(200)
select @sum1=case @是否加横向合计
when 1 then ',[合计]=sum(['+@表体内容+'])'
else '' end
,@sum2=case @是否家纵向合计
when 1 then '['+@纵轴+']=case grouping(['
+@纵轴+']) when 1 then ''合计'' else cast(['
+@纵轴+'] as varchar) end'
else '['+@纵轴+']' end
,@sum3=case @是否家纵向合计
when 1 then ' with rollup'
else '' end
--生成交叉表
exec('select '+@sum2+@sql+@sum1+'
from ['+@TableName+']
'+@条件+'
group by ['+@纵轴+']'+@sum3)
go
select case when showname='name1' then value end as value1,
case when showname='name2' then value end as value2,
case when showname='name3' then value end as value3,
case when showname='name4' then value end as value4
from table1
select case when showname='name1' then value end as value1,
case when showname='name2 then value end as value2,
case when showname='name3 then value end as value3,
case when showname='name4 then value end as value4
from table1
select case when showname='name1' then value as value1,
case when showname='name2 then value as value2,
case when showname='name3 then value as value3,
case when showname='name4 then value as value4
from table1