Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tsql 将空值更改为零(透视表)_Tsql_Pivot Table - Fatal编程技术网

Tsql 将空值更改为零(透视表)

Tsql 将空值更改为零(透视表),tsql,pivot-table,Tsql,Pivot Table,我创建了一个存储过程,它创建了数据透视表,如下所示。问题是,它不能像预期的那样工作,因为它不会将空值更改为零。你能告诉我正确的方向吗 create procedure sp_system_counts as declare @columns nvarchar(max) declare @columnscondition nvarchar(max) declare @query nvarchar(max) select distinct systemgroup, systemgroupsort

我创建了一个存储过程,它创建了数据透视表,如下所示。问题是,它不能像预期的那样工作,因为它不会将空值更改为零。你能告诉我正确的方向吗

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

我不确定这里实现的逻辑是什么,但如果您更改以下内容,它可能会有所帮助:

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

i、 e.在数据透视之前,而不是在数据透视之前,对数据透视执行isnull..0

select @columns = isnull(@columns + ',', '') + 'isnull(' + quotename(convert(varchar, systemgroup)) + ', 0) as ' + quotename(convert(varchar, systemgroup)) FROM #temp_table_system_group
select @columnscondition =  + isnull(@columnscondition + ' or ', '') + 'isnull(' + quotename(convert(varchar, systemgroup)) + ', 0) <> 0' FROM #temp_table_system_group