日期:2014-05-17 浏览次数:20509 次
create table a(编码 varchar(10), 尺寸 varchar(10), 数量 int)
insert into a
select '01', '0.2', 2 union all
select '01', '0.2', 3 union all
select '01', '0.3', 5 union all
select '02', '0.3', 1
go
declare @sql varchar(8000)
set @sql = ''
select @sql = @sql +
',sum(case when 尺寸='''+CAST(尺寸 as varchar)+''' then 数量 else 0 end) as [' +
CAST(尺寸 as varchar)+']'
from a
group by 尺寸
select @sql = 'select 编码' + @sql + ',sum(数量) as 小计 from a group by 编码'
exec(@sql)
/*
编码 0.2 0.3 小计
01 5 5 10
02 0 1 1
*/