create table XJB(XH varchar(10),DC varchar(10),ZY varchar(10))
insert into XJB select '0301',北京','物理'
insert into XJB select '0201',北京','化学'
insert into XJB select '0203',北京','物理'
insert into XJB select '0301',上海','中文'
insert into XJB select '0302',上海','中文'
insert into XJB select '0405',杭州','物理'
insert into XJB select '0402',南京','数学'
declare @s varchar(8000)
set @s = ''
select @s = @s + ','+DC+'=sum(case when DC='''+DC+''' then 1 else 0 end)' from XJB group by DC
exec('select ZY'+@s+' from XJB group by ZY')
insert into XJB select '0301',北京','物理'
insert into XJB select '0201',北京','化学'
insert into XJB select '0203',北京','物理'
insert into XJB select '0301',上海','中文'
insert into XJB select '0302',上海','中文'
insert into XJB select '0405',杭州','物理'
insert into XJB select '0402',南京','数学'
declare @s varchar(8000)
set @s = ''
select @s = @s + ','+DC+'=sum(case when DC='''+DC+''' then 1 else 0 end)' from XJB group by DC
exec('select ZY'+@s+' from XJB group by ZY')
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货