這種情況只能用動態sql語句才能寫好﹗給你一個例子看看
create table tb(名稱 varchar(10),數量  numeric(10),類型 varchar(5))
Insert into tb 
select 'L001','1','A'
union all select 'L001','2','B'
union all select 'L002','5','C'
union all select 'L003','6','D'
union all select 'L004','9','A'
union all select 'L004','5','D'select * from tbdeclare @sql varchar(1000)
set @sql=''
select @sql=@sql+',['+max(類型)+']=sum(case 類型 when '''+max(類型)+''' then 數量 else 0 end)'
from tb group by 類型 --order by 類型
print @sqlexec('select 名稱'+@sql+' from  tb  group by 名稱')
--結果
名稱     A         B        C        D
----------------------------------------
L001 1 2 0 0
L002 0 0 5 0
L003 0 0 0 6
L004 9 0 0 5