如何由
表1
office procode fact_qty
bj p1 3
bj p2 4
bj p3 8
sh p1 2
sh p2 5
sh p3 9
得到
表2
procode bj sh
p1 3 2
p2 4 5
p3 8 9
office 的内容会变化
表2可以动态生成吗
谢了
create table A
(
office varchar(10),
procode varchar(10),
fact_qty int
)
insert A
select bj,p1,3 union
select bj,p2,4 union
select bj,p3,8 union
select sh,p1,2 union
select sh,p2,5 union
select sh,p3,9
--查询
declare @sql varchar(8000)
set @sql=
select @sql=@sql+,sum(case when office=+office+ then fact_qty else 0 end) as +office+
from A group by office
select @sql=select procode+@sql+ from A group by procode
exec(@sql)
--删除测试环境
drop table A
--结果
/*
procode bj sh
---------- ----------- -----------
p1 3.00 2.00
p2 4.00 5.00
p3 8.00 9.00
*/