0
다음과 같이 피벗 테이블을 작성하는 저장 프로 시저를 작성했습니다. 문제는 null을 0으로 변경하지 않기 때문에 예상대로 작동하지 않는다는 것입니다. 내게 올바른 방향을 보여 주시겠습니까?널 (NULL)을 0으로 변경하십시오. (피봇 테이블)
create procedure sp_system_counts
as
declare @columns nvarchar(max)
declare @columnscondition nvarchar(max)
declare @query nvarchar(max)
select distinct systemgroup, systemgroupsortorder into #temp_table_system_group
from systemgroups
where systemgroup not like 'N/A'
order by systemgroupsortorder
select @columns = isnull(@columns + ',', '') + '[' + convert(varchar, systemgroup) + ']' FROM #temp_table_system_group
select @columnscondition = + isnull(@columnscondition + ' or ', '') + '[' + convert(varchar, systemgroup) + '] <> 0' FROM #temp_table_system_group
--select @columns
create table #temp_systems (
systemid int,
systemnane varchar(max),
region varchar(50),
systemgroup varchar(50),
remsid int,
remscode int)
insert into #temp_systems
select distinct sy.systemid, sy.systemname, co.region, syg.systemgroup, re.remsid, re.remscode
from systems sy
inner join servers se on sy.systemid = se.systemid and sy.systemid = sy.systemid
inner join rems re on se.remsid = re.remsid
inner join cities ci on re.cityid = ci.cityid
inner join countries co on ci.countryid = co.countryid
inner join systemmodels sym on sy.systemmodelid = sym.systemmodelid
inner join systemtypes syt on sym.systemtypeid = syt.systemtypeid
inner join systemgroups syg on syt.systemgroupid = syg.systemgroupid
where syg.systemgroup not like 'N/A'
order by syg.systemgroup
set @query = '
select region, ' + @columns + '
from (
select distinct region, systemgroup, cnt = isnull(count(systemid),0) from #temp_systems
group by region, systemgroup
with rollup) p
pivot (sum (cnt) for systemgroup in (' + @columns + ')) as asd
where (' + @columnscondition +')'
execute(@query)
drop table #temp_table_system_group
drop table #temp_systems