有以下数据类别 名称 数量 金额 机构
0206 胃肠道类 18564.790 2682.310 202
0206 胃肠道类 35785.870 4255.970 203
0206 胃肠道类 33076.470 4356.490 205
0206 胃肠道类 91597.800 12534.530 206
0206 胃肠道类 32627.620 4542.720 207
0206 胃肠道类 6458.160 1080.050 208
0206 胃肠道类 8242.740 1863.310 209
0206 胃肠道类 2545.130 389.220 210
0206 胃肠道类 5788.650 848.810 201
想实现如下效果:
类别 名称 201数量 201金额 202数量 202金额 .......
0206 胃肠道类 5788.650 848.810 18564.790 2682.310
0206 胃肠道类 18564.790 2682.310 202
0206 胃肠道类 35785.870 4255.970 203
0206 胃肠道类 33076.470 4356.490 205
0206 胃肠道类 91597.800 12534.530 206
0206 胃肠道类 32627.620 4542.720 207
0206 胃肠道类 6458.160 1080.050 208
0206 胃肠道类 8242.740 1863.310 209
0206 胃肠道类 2545.130 389.220 210
0206 胃肠道类 5788.650 848.810 201
想实现如下效果:
类别 名称 201数量 201金额 202数量 202金额 .......
0206 胃肠道类 5788.650 848.810 18564.790 2682.310
create table test (姓名 char(10),课程 char(10),成绩 int)
go
insert test values('张三','语文',80)
insert test values('张三','数学',86)
insert test values('张三','英语',75)
insert test values('李四','语文',78)
insert test values('李四','数学',85)
insert test values('李四','英语',78)
select * from test
declare @sql varchar(8000)
set @sql = 'select 姓名'
select @sql = @sql + ',sum(case 课程 when '''+课程+''' then 成绩 end) ['+课程+']'
from (select distinct 课程 from test) as a
select @sql = @sql+' from test group by 姓名'
exec(@sql)drop table test--结果
/*
姓名 课程 成绩
张三 语文 80
张三 数学 86
张三 英语 75
李四 语文 78
李四 数学 85
李四 英语 78姓名 数学 英语 语文
李四 85 78 78
张三 86 75 80
*/
drop table tb
gocreate table tb
(
类别 varchar(10),
名称 varchar(10),
数量 decimal(18,3),
金额 decimal(18,3),
机构 varchar(10)
)insert into tb(类别,名称,数量,金额,机构) values('0206','胃肠道类',18564.790,2682.310,'202')
insert into tb(类别,名称,数量,金额,机构) values('0206','胃肠道类',35785.870,4255.970,'203')
insert into tb(类别,名称,数量,金额,机构) values('0206','胃肠道类',33076.470,4356.490,'205')
insert into tb(类别,名称,数量,金额,机构) values('0206','胃肠道类',91597.800,12534.530,'206')
insert into tb(类别,名称,数量,金额,机构) values('0206','胃肠道类',32627.620,4542.720,'207')
insert into tb(类别,名称,数量,金额,机构) values('0206','胃肠道类',6458.160 ,1080.050,'208')
insert into tb(类别,名称,数量,金额,机构) values('0206','胃肠道类',8242.740 ,1863.310,'209')
insert into tb(类别,名称,数量,金额,机构) values('0206','胃肠道类',2545.130 ,389.220, '210')
insert into tb(类别,名称,数量,金额,机构) values('0206','胃肠道类',5788.650 ,848.810, '201') declare @sql varchar(8000)
set @sql = 'select 类别,名称'
select @sql = @sql + ' , max(case 机构 when ''' + 机构 + ''' then 数量 end) [' + 机构 + '数量]'
+ ' , max(case 机构 when ''' + 机构 + ''' then 金额 end) [' + 机构 + '金额]'
from (select distinct 机构 from tb) as a
set @sql = @sql + ' from tb group by 类别,名称'
exec(@sql) drop table tb类别 名称 201数量 201金额 202数量 202金额 203数量 203金额 205数量 205金额 206数量 206金额 207数量 207金额 208数量 208金额 209数量 209金额 210数量 210金额
---- -------- -------- ------- --------- -------- --------- -------- --------- -------- --------- --------- --------- -------- -------- -------- -------- -------- -------- -------
0206 胃肠道类 5788.650 848.810 18564.790 2682.310 35785.870 4255.970 33076.470 4356.490 91597.800 12534.530 32627.620 4542.720 6458.160 1080.050 8242.740 1863.310 2545.130 389.220