--------------统计一列的某些行进行统计并将统计值作为列显示-----------------
--创建表test
create table test
(str_date int,
str_num int,
str_name varchar(10)
)
--插入数据
insert into test
select 1,34,'A' union all
select 1,21,'B' union all
select 2,23,'D' union all
select 2,32,'C' union all
select 3,56,'D' union all
select 3,19,'A' union all
select 4,26,'B' union all
select 4,38,'A'
--进行查询
declare @sqlstr varchar(8000)
select @sqlstr = ''
select @sqlstr = @sqlstr +',sum(case str_name when '''+str_name+''' then str_num else 0 end) as '+ str_name
from test
group by str_name
exec('select str_date'+@sqlstr+' from test group by str_date')
--结果显示
strdate A