日期:2014-05-17 浏览次数:20510 次
use tempdb
go
if OBJECT_ID('Tempdb..#1') is not null
drop table #1
Create table #1(订单 varchar(10), 存货名称 varchar(10), 工序名 varchar(10), 数量 int)
insert into #1
select '0001', 'aaaa', 'a', 1000 union all
select '0001', 'aaaa', 'b', 2000 union all
select '0001', 'aaaa', 'c', 3000
go
declare @sql nvarchar(2000)
set @sql='select 订单,存货名称'
select @sql=@sql+','+QUOTENAME(工序名)+'=max(case when [工序名]='+QUOTENAME(工序名,'''')+' then 数量 else 0 end)'
from #1 group by 工序名
exec(@sql+' from #1 group by 订单,存货名称')
/*
订单 存货名称 a b c
0001 aaaa 1000 2000 3000
*/