34,590
社区成员
发帖
与我相关
我的任务
分享
select id,max(东部)东部,max(北部)北部,max(中部)中部,max(西部)西部
from(
select
id='vaim',sort=1,
东部=case when vOrgid='东部' then vaim end,
北部=case when vOrgid='北部' then vaim end,
中部=case when vOrgid='中部' then vaim end,
西部=case when vOrgid='西部' then vaim end
from 表
union all
select
id='vqqTvalue',sort=2,
东部=case when vOrgid='东部' then vqqTvalue end,
北部=case when vOrgid='北部' then vqqTvalue end,
中部=case when vOrgid='中部' then vqqTvalue end,
西部=case when vOrgid='西部' then vqqTvalue end
from 表
union all
select
id='vqppValue',sort=3,
东部=case when vOrgid='东部' then vqppValue end,
北部=case when vOrgid='北部' then vqppValue end,
中部=case when vOrgid='中部' then vqppValue end,
西部=case when vOrgid='西部' then vqppValue end
from 表
) t group by id order by max(sort)
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id int,vOrgid varchar(4),vaim int,vqqTvalue int,vqppValue int)
insert into [tb]
select 1,'东部',7660,3874,211 union all
select 2,'北部',6872,2776,165 union all
select 3,'中部',9893,4760,260 union all
select 4,'西部',7076,3184,109
go
declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000)
select @f1='',@f2='',@f3=''
select @f1=@f1+',['+vOrgid+']='''+ltrim(vaim)+''''
,@f2=@f2+','''+ltrim(vqqTvalue)+''''
,@f3=@f3+','''+ltrim(vqppValue)+''''
from tb
exec('select [col]=''vaim'''+@f1
+' union all select ''vqqTvalue'''+@f2
+' union all select ''vqppValue'''+@f3)
go
--结果:
col 东部 北部 中部 西部
--------- ---- ---- ---- ----
vaim 7660 6872 9893 7076
vqqTvalue 3874 2776 4760 3184
vqppValue 211 165 260 109
type 东部 北部 中部 西部
------- ------ ------- ------- ------
vaim 7660 6872 9893 7076
vqppValue 211 165 260 109
vqqTvalue 3874 2776 4760 3184
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id int,vOrgid varchar(4),vaim int,vqqTvalue int,vqppValue int)
insert into [tb]
select 1,'东部',7660,3874,211 union all
select 2,'北部',6872,2776,165 union all
select 3,'中部',9893,4760,260 union all
select 4,'西部',7076,3184,109
go
select [type], isnull(东部,0) 东部, isnull(北部,0) 北部, isnull(中部,0) 中部, isnull(西部,0) 西部
from
(
select vorgid, [type], [value]
from
(
select vOrgid,vaim,vqqTvalue,vqppValue from tb) source unpivot
(
[value] for [type] in([vaim],[vqqTvalue], vqppValue)
)
unpvt
) t
pivot(max(value) for vorgid in([东部], [北部], [中部], [西部])) pvt
type 东部 北部 中部 西部----------------------------- ----------- ----------- ----------- -----------
vaim 7660 6872 9893 7076
vqppValue 211 165 260 109
vqqTvalue 3874 2776 4760 3184
(3 row(s) affected)
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id int,vOrgid varchar(4),vaim int,vqqTvalue int,vqppValue int)
insert into [tb]
select 1,'东部',7660,3874,211 union all
select 2,'北部',6872,2776,165 union all
select 3,'中部',null,4760,260 union all
select 4,'西部',7076,3184,109
go
--创建存储过程
create proc sp_wsp
as
declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000)
select @f1='',@f2='',@f3=''
select @f1=@f1+',['+vOrgid+']='''+isnull(ltrim(vaim),'')+''''
,@f2=@f2+','''+isnull(ltrim(vqqTvalue),'')+''''
,@f3=@f3+','''+isnull(ltrim(vqppValue),'')+''''
from tb
exec('select [col]=''vaim'''+@f1
+' union all select ''vqqTvalue'''+@f2
+' union all select ''vqppValue'''+@f3)
go
--调用存储过程
exec sp_wsp