在动态数据透视表中将Null替换为0

时间:2017-06-21 08:38:10

标签: sql-server tsql pivot dynamic-sql

我收到空值。我怎么能用0替换空值呢?我已尝试在pivot和select中使用isNull函数,但它似乎没有更新。 TY

SET @query ='SELECT * FROM(SELECT
      petstoreemployee.employeefirstname as employeefirstname
      ,sum(petID.breed) as breeds
      ,Format(date, ''MMMM-yyyy'') as Month

FROM
      petID, petstoreemployee
WHERE
      petID.petstoreemployeeID=petstoreemployee.petstoreemployeeID and
      petID.ProjectedPrjID=1
      and
      (petID.date >= ''2017-01-01 00:00:00:000'' AND petID.date <= 
''2017-12-31 00:00:00:000'')
  group by petstoreemployee.employeefirstname, Format(date,''yyyy'')

)
as d
PIVOT(
avg(breeds)
for employeefirstname
IN (' + @pet + ')
) as p'

1 个答案:

答案 0 :(得分:0)

在最外面的SELECT中使用ISNULL()。在您的情况下,SELECT * FROM (

之后的那个

如果不起作用,请将SELECT *替换为SELECT ISNULL(column1) etc...