日期:2014-05-18 浏览次数:20498 次
--> 测试数据: #A if object_id('tempdb.dbo.#A') is not null drop table #A create table #A (A varchar(11),B varchar(11),C varchar(11),D int) insert into #A select 'w','f','ff',100 union all select '2','df','df',200 union all select 'ff','ff','ff',300 select * from (select C,D from #A) a pivot (sum(D) for C in (ff,df)) b /* ff df ----------- ----------- 400 200 */
------解决方案--------------------
create table #A (A varchar(11),B varchar(11),C varchar(11),D int) insert into #A select 'w','f','ff',100 union all select '2','df','df',200 union all select 'ff','ff','ff',300 select df=max(case when c='df' then d end),ff=max(case when c='ff' then d end) from ( select C,sum(D) D from #A group by C ) T drop table #A
------解决方案--------------------