1

下面的代码成功生成了一个Pivot(如下代码所示)

select * 
from (
Select GLAccount , DC , Amount  from AccountingTxns a where Acctno = '178523'
)
TableData
pivot (
sum (Amount)
for DC in ([D],[C])
) 
as SumAmountDC
GLAccount DC
利息到期 3801.37 3731.68
现金 25600 25000
应收账款 3801.37 3801.37
收入 NULL 3684.47
IntOverdue NULL 116.9
校长 25000 21868.32

我如何摆脱 NULL(即将它们转换为 0)?

4

1 回答 1

2

使用COALESCE

SELECT  GLAccount, 
        COALESCE(D, 0) D, 
        COALESCE(C, 0) C
FROM 
        (
            SELECT  GLAccount, 
                    DC , 
                    Amount  
            FROM    AccountingTxns a 
            WHERE   Acctno = '178523'
        ) TableData
        PIVOT
        (
            SUM(Amount)
            FOR DC IN ([D],[C])
        ) SumAmountDC
于 2013-04-05T05:04:06.440 回答