如何使用动态SQL替换Pivot中的null
可能重复:如何使用动态SQL替换Pivot中的null,sql,null,pivot,Sql,Null,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],[Constr
我正在使用以下动态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附近说不正确的syantax。有没有办法解决这个问题
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
此外,我的数据中没有任何空值,但是当我使用pivot时,结果显示为空值。不知道为什么
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
答案如下:
在旋转临时表之前,先准备临时表中的数据
ISNULL(FieldName, '0') as FieldName
我试过了,但没用。你的空值从哪里来?你能给我一份临时表的样品吗?我不确定。使用轴时,将显示空值。我认为这与我的数据有关。对于某些区域,该部分可能不会出现任何损失,因此我猜默认情况下它会在那里放置一个null。您是否能够提供临时表的示例?查看包含一些数据的结构将非常有帮助。如果其中包含敏感信息,则可以更改这些值。我们仍然可以找到问题。我只是在我的问题中展示了上面的样本输出和数据。我需要查看一些样本数据以了解问题所在。