简单行转列问题
我已经通过存储过程取得了报表的数据,格式如下:
工厂 月份1 月份2 ... 到月份N //月份是根据客户查询条件的
工厂1 销售额 销售额 销售额
工厂2 销售额 销售额 销售额
.
.
.
工厂N ...
表示一个大工厂下面有多个小工厂, 通过条件查各个工厂某几个月的销售额
数据已经取出来了,但是要显示成下面格式:
月份 工厂1 工厂2 ... 工厂N
月份1 销售额 销售额 ...
月份2 销售额 销售额
...
月份N ...
怎么写
[解决办法]
行列转换实例:
--建境
Create Table TEST
(SPBM Nvarchar(20),
YGTJS Int,
SJTJS Int,
BFL Nvarchar(20))
--插入
Insert TEST Select N '技部 ', 75, 51, '68% '
Union All Select N '材部 ', 29, 29, '100% '
Union All Select N '生部 ', 1069, 821, '77% '
GO
--
Declare @SPJS Varchar(8000), @SPBM Varchar(8000)
Select @SPJS = ' ', @SPBM = ' '
Select @SPJS = @SPJS + ' Union All Select SPBM, ' + Cast(ColID As Varchar) + ' As ColID, ' ' ' + Name + ' ' ' As SPJS, Cast( ' + Name + ' As Varchar) As [ALLSPJS] From TEST '
From SysColumns Where ID = OBJECT_ID( 'TEST ') And Name != 'SPBM ' Order By ColID
Select @SPJS = Stuff(@SPJS, 1, 10, ' ')
Select @SPBM = @SPBM + ', Max(Case SPBM When ' ' ' + SPBM + ' ' ' Then ALLSPJS Else ' ' ' ' End) As ' + SPBM From TEST Group By SPBM
Print @SPJS
Print @SPBM
EXEC( ' Select SPJS ' + @SPBM + ' From ( ' + @SPJS + ' ) A Group By SPJS Order By Min(ColID) ')
GO
--除境
Drop Table TEST
[解决办法]
统计工厂数,
建一临时表保存结果,以列数为工厂数
行数为1-12之间(随楼主所需)
再根据已知数的数据,对此表进行update
[解决办法]
--此为将表旋转90度.
--以下三例均可满足楼主的要求.
将下表数据:
A b c d e
-------------------- ----------- ----------- ----------- -----------
x 1 2 3 4
y 5 6 7 8
z 9 10 11 12
转化成如下结果:
a x y z
-------------------- ---------- ---------- ----------
b 1 5 9
c 2 6 10
d 3 7 11
e 4 8 12
--生成测试数据
create table test1(A varchar(20),b int,c int,d int,e int)
insert into test1 select 'x ',1,2 ,3 ,4
insert into test1 select 'y ',5,6 ,7 ,8
insert into test1 select 'z ',9,10,11,12
--生成中间数据表
declare @s varchar(8000)
set @s= 'create table test2(a varchar(20) '
select @s=@s+ ', '+A+ ' varchar(10) ' from test1
set @s=@s+ ') '
exec(@s)
--借助中间表实现行列转换
declare @name varchar(20)
declare t_cursor cursor for
select name from syscolumns
where id=object_id( 'test1 ') and colid> 1 order by colid
open t_cursor
fetch next from t_cursor into @name
while @@fetch_status=0
begin
exec( 'select '+@name+ ' as t into test3 from test1 ')
set @s= 'insert into test2 select ' ' '+@name+ ' ' ' '
select @s=@s+ ', ' ' '+rtrim(t)+ ' ' ' ' from test3
exec(@s)
exec( 'drop table test3 ')
fetch next from t_cursor into @name
end
close t_cursor
deallocate t_cursor
--查看行列互换处理结果
select * from test1
select * from test2
[解决办法]
create table tb(工厂 varchar(10),月份01 int,月份02 int,月份03 int,月份04 int,
月份05 int,月份06 int,月份07 int,月份08 int,
月份09 int,月份10 int,月份11 int,月份12 int)
insert into tb values( '工厂1 ',1,2,3,4,5,6,7,8,9,10,11,12)
insert into tb values( '工厂2 ',21,22,23,24,25,26,27,28,29,210,211,212)
go
create proc p_zj
@tbname sysname, --要处理的表名
@fdname sysname, --做为转换的列名
@new_fdname sysname= ' ' --为转换后的列指定列名
as
declare @s1 varchar(8000),@s2 varchar(8000)
,@s3 varchar(8000),@s4 varchar(8000),@s5 varchar(8000)
,@i varchar(10)
select @s1= ' ',@s2= ' ',@s3= ' ',@s4= ' ',@s5= ' ',@i= '0 '
select @s1=@s1+ ',@ '+@i+ ' varchar(8000) '
,@s2=@s2+ ',@ '+@i+ '= ' ' '+case isnull(@new_fdname, ' ') when ' ' then ' '
else @new_fdname+ '= ' end+ ' ' ' ' ' '+name+ ' ' ' ' ' ' ' '
-- ,@s2=@s2+ ',@ '+@i+ '= ' '性别= ' ' ' ' '+name+ ' ' ' ' ' ' ' '
,@s3=@s3+ '
select @ '+@i+ '=@ '+@i+ '+ ' ',[ ' '+[ '+@fdname+ ']+ ' ']= ' '+cast([ '+name+ '] as varchar) from [ '+@tbname+ '] '
,@s4=@s4+ ',@ '+@i+ '= ' 'select ' '+@ '+@i
,@s5=@s5+ '+ ' ' union all ' '+@ '+@i
,@i=cast(@i as int)+1
from syscolumns
where object_id(@tbname)=id and name <> @fdname
select @s1=substring(@s1,2,8000)
,@s2=substring(@s2,2,8000)
,@s4=substring(@s4,2,8000)
,@s5=substring(@s5,16,8000)
exec( 'declare '+@s1+ '
select '+@s2+@s3+ '
select '+@s4+ '
exec( '+@s5+ ') ')
go
exec p_zj 'tb ', '工厂 ', '月份 '
drop table tb
drop proc p_zj
/*
月份 工厂1 工厂2
------ ----------- -----------
月份01 1 21
月份02 2 22
月份03 3 23
月份04 4 24
月份05 5 25
月份06 6 26
月份07 7 27
月份08 8 28
月份09 9 29
月份10 10 210
月份11 11 211
月份12 12 212
(所影响的行数为 12 行)
*/