枢轴和空值

时间:2013-11-07 18:32:33

标签: tsql

在这个简单的数据透视示例(T-SQL)中,我试图用0替换NULL。我已经尝试了我找到但仍然为NULL的所有建议。如何用0

替换PIVOT中的NULL值
select *
from 
 (
       select ISNULL([vendid],0)as [vendid],isnull(origdocamt,0)as origdocamt 
        from APDoc
 ) X
pivot
(sum(origdocamt) for vendid in ([AAA],[BBB])) As P

输出


AAA BBB

45800 NULL

2 个答案:

答案 0 :(得分:3)

您需要使用IsNullCoalesce在最终选择中替换null

select 
  [AAA] = IsNull([AAA], 0),
  [BBB] = IsNull([BBB], 0)
from 
(
  select [vendid],
    origdocamt 
  from APDoc
) X
pivot
(
  sum(origdocamt) 
  for vendid in ([AAA],[BBB])
) As P

答案 1 :(得分:0)

您看到的NULL是因为新值:

select ISNULL([AAA],0) as [AAA], ISNULL([BBB],0) as [BBB]
from 
 (
       select ISNULL([vendid],0)as [vendid],isnull(origdocamt,0)as origdocamt 
        from APDoc
 ) X
pivot
(sum(origdocamt) for vendid in ([AAA],[BBB])) As P