sql中如何将某列的行内容变成列字段名

如题所述

if object_id('tb')is not null drop table tbgocreate table tb(金额 money, 日期 varchar(10), 名称 varchar(10)) insert tb select 100 , '200802', 'abc' insert tb select 85 , '200802' , 'def' insert tb select 150, '200812' , 'abc' insert tb select 0 , '200812' ,'def'select 名称,sum(case when 日期='200802' then 金额 else 0 end)[200802],sum(case when 日期='200812' then 金额 else 0 end)[200812] from tb group by 名称/*名称 200802 200812 ---------- --------------------- --------------------- abc 100.0000 150.0000def 85.0000 .0000*/
温馨提示:答案为网友推荐,仅供参考