Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL中替换PIVOT中的null_Sql_Null_Pivot - Fatal编程技术网

如何在SQL中替换PIVOT中的null

如何在SQL中替换PIVOT中的null,sql,null,pivot,Sql,Null,Pivot,我有下面的代码,我试图用零替换使用pivot时出现的Null。我做了以下操作,但它指出“ISNULL”附近的语法不正确。我不确定我做错了什么?有什么建议吗 select * from #tempfinaltable pivot ISNULL(sum(TotalXSAAL),0) for Section_desc in ([Communication],[Construction],[Energy],[Financial Institutions], [General Property],[HI

我有下面的代码,我试图用零替换使用pivot时出现的Null。我做了以下操作,但它指出“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
我使用的动态SQL也是如此。上面的查询只是显示名称,以便您可以看到我正在使用的内容

 select *
from #tempfinaltable
pivot (sum(TotalXSAAL) for Section_desc in
' + '('+@BranchNames++')) AS AALs'
你能告诉我这句话有什么不对吗。我有一个语法问题:

BEGIN 

    Set @ISNullBranchNames = @ISNullBranchNames + 'ISNULL('+(@BranchNames+',0),' 
    Set @BranchNames = @BranchNames + '['+@BranchName+'],'

    FETCH NEXT FROM CUR1 INTO @BranchName

END
所有PIVOT子句必须放在括号中

结果查询必须如下所示:

SELECT 
      'TotalXSAAL' as Col,
      ISNULL([Communication], 0) AS [Communication],
      ISNULL([Construction], 0) AS [Construction],
      ...,
      ...,
      ... 
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
更新

如何生成部分动态SQL

DECLARE @ISNullBranchNames nvarchar(MAX) = N'' -- you mast add empty string first, otherwise you will get NULL inresult
DECLARE @BranchNames nvarchar(MAX) = N''
.....
BEGIN 

    Set @ISNullBranchNames =
             @ISNullBranchNames + 'ISNULL([' + @BranchName + '], 0) AS [' + @BranchName +'], '
    Set @BranchNames = @BranchNames + '['+@BranchName+'],'

    FETCH NEXT FROM CUR1 INTO @BranchName

END
若要用空字符串替换透视表中的NULL,则

ISNULL ( CAST (SUM(UnitPrice]) as NVARCHAR),'') 

这会将单价的总和值转换为VARCHAR,然后使用ISNULL将其替换为空字符串,如果我运行以下代码,它会起作用:从tempfinaltable pivot sumTotalXSAAL中为中的部分_desc选择*[通讯],[建筑],[能源],[金融机构],[一般财产],[高等教育与医疗保健],[内陆海洋],[房地产]作为AALs,但我正在尝试替换运行此查询时出现的空值。我尝试了建议的内容,但仍然不起作用。真正的可能是我编写了一个动态SQL查询,我正在尝试使其起作用。我上面的内容是我打印的内容,以便显示我的身份使用。我如何用动态SQL替换PIVOT中的空值?请从tempfinaltable PIVOT ISNULLsumTotalXSAAL中选择*0作为“++@BranchNames++”AS AALs”中的部分描述。哈姆雷特,您提供的查询没有执行我尝试执行的操作。我正在尝试为eac显示h节,按区域损失的数字。您的查询显示了每个节的总损失。我的查询显示了按区域损失的每个节的数字。只想将null替换为零。我很困惑。我不是很清楚这一点。@user2029109@BranchNames是以逗号分隔的数据透视列列表[通信],[构造],…您必须生成@BranchNamesWrappedByISNULL,如下所示:ISNULL[Communication],0作为[Communication],ISNULL[Construction],0作为[Construction],…并将其放入主选择列表中。