0

可能重复:
如何在 SQL 中替换 PIVOT 中的空值

我正在使用以下动态 SQL:

 select *
    from #tempfinaltable
    pivot (sum(TotalXSAAL) for Section_desc in
    ' + '('+@BranchNames++')) AS AALs'

这与上面的查询相同

select *
from #tempfinaltable
pivot (sum(TotalXSAAL) for Section_desc in
([Communication],[Construction],[Energy],[Financial Institutions],
 [General Property],[HIGHER ED & HEALTHCARE],
 [Inland Marine],[Real Estate])) AS AALs

我想做的是在运行此查询时将空值替换为零。

我尝试使用 ISNULL 以便可以将其替换为零,但它不起作用。在 IsNull 附近说不正确的语法。任何想法如何解决它?

select *
from #tempfinaltable
pivot IsNull((sum(TotalXSAAL),0) for Section_desc in
([Communication],[Construction],[Energy],[Financial Institutions],
 [General Property],[HIGHER ED & HEALTHCARE],
 [Inland Marine],[Real Estate])) AS AALs

我也尝试过这样做,但也没有用。

select Section_Desc, Zone, ISNULL(Sum(TotalXSAAL),0)
from #tempfinaltable
pivot (sum(TotalXSAAL) for Section_desc in
([Communication],[Construction],[Energy],
 [Financial Institutions],[General Property],
 [HIGHER ED & HEALTHCARE],[Inland Marine],
 [Real Estate])) AS AALs

此外,我的数据中没有任何空值,但是当我使用数据透视表时,结果显示为空值。不知道为什么。

Example Data
Section_Desc    Zone    TotalXSAAL
Energy           Zone1  0
Energy           Zone2  0
Energy           Zone10 452
Energy           Zone2  1123
Energy            Zone3 87

Sameple Ouput:
ZONE    Communication   Construction    Energy  Financial Institutions  General  property   Higher Ed & Healthcare  Inland Marine   Real estate
ZONE1   10221   NULL    42124   89214211    911243  NULL    123120  1234235

答案在这里: How to Replace Nulls in PIVOT in SQL

4

1 回答 1

0

在对临时表进行透视之前准备好临时表中的数据。

ISNULL(FieldName, '0') as FieldName
于 2013-01-31T13:45:55.700 回答