日期:2014-05-18 浏览次数:20552 次
--> 测试数据: #A if object_id('tempdb.dbo.#A') is not null drop table #A create table #A (名称 varchar(4),门店 varchar(3),数量 int) insert into #A select 'aaa1','aaa',10 union all select 'bbb1','bbb',12 union all select 'aaa1','bbb',4 union all select 'bbb1','aaa',6 union all select 'ccc1','aaa',2 union all select 'ccc1','bbb',1 union all select 'ccc1','aaa',1 union all select 'aaa1','aaa',1 union all select 'bbb1','bbb',1 union all select 'ccc1','bbb',1 --> SQL 2005 select * from (select * from #A) as a pivot (sum(数量) for 名称 in (aaa1,bbb1,ccc1)) as b /* 门店 aaa1 bbb1 ccc1 ---- ----------- ----------- ----------- aaa 11 6 3 bbb 4 13 2 */