替换 sql pivot 中的空值sql、pivot

2023-09-07 17:46:52 作者:带着皇冠藐视你

我有以下疑问:

SELECT *
FROM Table1
PIVOT
(
  SUM(Value)
  FOR [Period] IN ([06/1/2007],[07/1/2007])
)
AS p

返回的某些行是空的,但我想用 0 替换它们.

Some of the rows returned are null but i want to replace them with 0.

我试过 SUM(ISNULL(Value,0)) as Val 但它不起作用.(它说的语法不正确)

I've tried SUM(ISNULL(Value,0)) as Val but it's not working. ( it's saying incorrect syntax)

推荐答案

哦,我在错误的地方使用了 ISNULL.

Ohh, I was using ISNULL in the wrong place.

查询应如下所示:

SELECT ID,ISNULL([06/1/2007],0), ISNULL([07/1/2007],0)
FROM Table1
PIVOT
(
  SUM(Value)
  FOR [Period] IN ([06/1/2007],[07/1/2007])
)
AS p