SQL 服务器的 Dynamic Pivot 中的 NULL 值返回“0”

Returning "0" for NULL values within Dynamic Pivot for SQL Server

我有以下代码:

DECLARE @cols AS NVARCHAR(MAX),                 
    @query AS NVARCHAR(MAX)         

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(month)                   
               from PRCombinedRM    
               group by month,AccountNumber 
        FOR XML PATH(''), TYPE      
        ).value('.', 'NVARCHAR(MAX)')       
    ,1,1,'')            

set @query = 'SELECT AccountNumber,' + 'FullName,' + 'AccountType,' + 'Company,' + 'AccountBalance,' + @cols + ' from                   
         (      
            select AccountNumber,   
                   FullName,
                   AccountType,
                   Company,
                   AccountBalance,
                   month,
                   amount
                from PRCombinedRM
            ) x 
            pivot   
            (   
                sum(amount)
                for month in (' + @cols + ')
            ) p '   

execute(@query)                 

但是目前输出的结果显示 "amount" 的值为 NULL,但是我想用“0”代替 NULL 值。 我该怎么做?

目前数据输出如下:

AccountNumber   FullName    AccountType Company AccountBalance  Aug     Jul     Jun     Sep 
100 M R Test    Test Account    Test Company    100 -50 -50 NULL    -50

但是我希望数据输出为:

AccountNumber   FullName    AccountType Company AccountBalance  Aug     Jul     Jun     Sep 
100 M R Test    Test Account    Test Company    100 -50 -50 0   -50

谢谢。

您可以更改 @cols 定义:

SET @cols = STUFF((SELECT distinct ', coalesce(' + QUOTENAME(month) ', 0) as ' + QUOTENAME(month)                 
               from PRCombinedRM    
               group by month,AccountNumber 
        FOR XML PATH(''), TYPE      
        ).value('.', 'NVARCHAR(MAX)')       
    , 1, 2,'')            

我会使用另一个变量来存储 ISNULL(someColumn,0):

DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX);
DECLARE @cols2 AS NVARCHAR(MAX)

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(month)                   
               from PRCombinedRM    
               group by month,AccountNumber 
        FOR XML PATH(''), TYPE      
        ).value('.', 'NVARCHAR(MAX)')       
    ,1,1,'');

SET @cols2 = STUFF((SELECT distinct ', ISNULL(' + QUOTENAME(month) + ',0) ' + QUOTENAME(month)
               from PRCombinedRM    
               group by month,AccountNumber 
        FOR XML PATH(''), TYPE      
        ).value('.', 'NVARCHAR(MAX)')       
    ,1,1,'');

set @query = 'SELECT AccountNumber,' + 'FullName,' + 'AccountType,' + 'Company,' + 'AccountBalance,' + @cols2 + ' from                   
         (      
            select AccountNumber,   
                   FullName,
                   AccountType,
                   Company,
                   AccountBalance,
                   month,
                   Amount
                from PRCombinedRM
            ) x 
            pivot   
            (   
                sum(amount)
                for month in (' + @cols + ')
            ) p ';

execute(@query);