日期:2014-05-17 浏览次数:20695 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (费别 nvarchar(6),金额 numeric(7,4),医生 nvarchar(6))
insert into [TB]
select '挂号费',300.0000,'医生甲' union all
select '放射费',600.0000,'医生甲' union all
select '治疗费',200.0000,'医生乙' union all
select '挂号费',300.0000,'医生乙' union all
select '西药费',40.0000,'医生丙' union all
select '中药费',30.0000,'医生丙' union all
select '治疗费',200.0000,'医生丙' union all
select '治疗费',200.0000,'医生丁'
select * from [TB]
declare @sql varchar(8000)
select @sql = isnull(@sql + '],[' , '') + 医生 from tb group by 医生
set @sql = '[' + @sql + ']'
exec ('select * from (select * from tb) a pivot (max(金额) for 医生 in (' + @sql + ')) b')
/*
费别 医生丙 医生丁 医生甲 医生乙
------ --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
放射费 NULL