【问题标题】:How to Replace nulls in Pivot using Dynamic SQL [duplicate]如何使用动态 SQL 替换 Pivot 中的空值 [重复]
【发布时间】:2013-01-31 13:38:21
【问题描述】:

可能重复:
How to Replace Nulls in PIVOT in SQL

我正在使用以下动态 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

【问题讨论】:

  • 需要查看一些示例数据来了解问题所在。

标签: sql null pivot


【解决方案1】:

在调整临时表之前准备好临时表中的数据。

ISNULL(FieldName, '0') as FieldName

【讨论】:

  • 我试过了,但是没有用。
  • 你的 NULLS 是从哪里来的?你能提供一个临时表的样本吗?
  • 我不确定。当我使用 Pivot 时,会出现空值。我认为这与我的数据有关。对于某些区域,该部分可能看起来没有任何损失,所以我猜它默认会放一个空值。
  • 你能提供临时表的样本吗?查看包含一些数据的结构将有很大帮助。如果其中包含敏感信息,您可以更改这些值。我们仍然可以找到问题。
  • 我刚刚在我的问题中展示了上面的示例输出和数据
猜你喜欢
  • 1970-01-01
  • 2013-01-15
  • 2012-10-09
  • 2018-04-15
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 2019-09-27
  • 2017-03-30
相关资源
最近更新 更多