如何使用sql server在数据透视表中将空值替换为0

时间:2017-08-28 07:36:44

标签: sql-server tsql

我试图在数据透视表中替换0.00代替null。查询如下

SELECT *
FROM (
    SELECT [dbo].[CLOI_ClientOrderItems].[cl_id] 'Customer Id'
        ,[dbo].[CL_Clients].[cl_name] 'Customer Name'
        ,FORMAT(in_date_issued, 'MMMM') AS 'Month'
        ,[dbo].[IN_Invoices].[in_total] AS 'Invoice_Total'
    FROM [dbo].[CLOI_ClientOrderItems]
    INNER JOIN [dbo].[CL_Clients] ON [dbo].[CLOI_ClientOrderItems].[cl_id] = [dbo].[CL_Clients].[cl_id]
    INNER JOIN [dbo].[IN_Invoices] ON [dbo].[CLOI_ClientOrderItems].[MasterOrderId] = [dbo].[IN_Invoices].[MasterOrderId]
    WHERE YEAR(in_date_issued) = @in_date_issued
        AND cl_system = 'DPO'
    ) AS s
PIVOT(Sum(Invoice_Total) FOR [Month] IN (
            january
            ,february
            ,march
            ,april
            ,may
            ,june
            ,july
            ,august
            ,september
            ,october
            ,november
            ,december
            )) AS pvt

1 个答案:

答案 0 :(得分:1)

只需使用ISNULL/COALESCE

SELECT col_names, 
       ISNULL(january, 0) AS january,
       COALESCE(february, 0) AS february,
       ...
FROM ...