如何在LEFT OUTER JOIN SQL Select语句中将NULL转换为0

时间:2018-10-05 16:05:03

标签: sql

我有一个SQL查询,该查询实现了LEFT OUTER JOIN来在一个表中编译不同的表。我希望能够将结果联接到同一个表中,但是要根据不同的参数(where子句)。

所以,这就是我要加入的表的结构:

dbo.Medios_Pauta

ID(PK-整数)

Nombre(varchar)

UsrCr(varchar)

FechaCr(日期时间)

dbo.N_Preinscripcion

ID(PK-整数)

Periodo(varchar)

MedioId(int)

...其他...

dbo.N_Insc_Preg_Control

ID(PK-整数)

IdPre(int-dbo.N_Preinscripcion.ID的前导键)

dbo.N_Insc_Posg_Control

ID(PK-整数)

IdPre(int-dbo.N_Preinscripcion.ID的前导键)

好,所以我建立的查询工作正常,但是只要有NULL值,它将在表中带来“ NULL”。我想将该NULL转换为零(0)。

我尝试使用ISNULL(COUNT(),0)和COALESCE(COUNT(),0),但它仍为NULL。

以下是带有ISNULL的查询代码:

SELECT
    [Inscritos_Preg].[MedioId],
    [Inscritos_Preg].[regs1],
    [Inscritos_Posg].[regs2],
    SUM([Inscritos_Preg].[regs1] + [Inscritos_Posg].[regs2])  AS [Inscritos]  
    FROM
        (
            SELECT
                [dbo].[Medios_Pauta].[ID] AS [MedioId],
                ISNULL(COUNT(*), 0) AS [regs1]
            FROM [dbo].[N_Preinscripcion]

            INNER JOIN
                [dbo].[Medios_Pauta]
                ON [dbo].[Medios_Pauta].[ID] = [dbo].[N_Preinscripcion].[MedioId] 

            INNER JOIN
                [dbo].[N_Insc_Preg_Control] 
                ON [dbo].[N_Insc_Preg_Control].[IdPre] = [dbo].[N_Preinscripcion].[ID]

            WHERE
                ([dbo].[N_Preinscripcion].[Periodo] = @prd)

            GROUP BY
                [dbo].[Medios_Pauta].[ID],
                [dbo].[Medios_Pauta].[Nombre]
        ) [Inscritos_Preg]
        LEFT OUTER JOIN
        (
            SELECT
                [dbo].[Medios_Pauta].[ID] AS [MedioId],
                ISNULL(COUNT(*), 0) AS [regs2]
            FROM [dbo].[N_Preinscripcion]

            INNER JOIN
                [dbo].[Medios_Pauta]
                ON [dbo].[Medios_Pauta].[ID] = [dbo].[N_Preinscripcion].[MedioId] 

            INNER JOIN
                [dbo].[N_Insc_Posg_Control] 
                ON [dbo].[N_Insc_Posg_Control].[IdPre] = [dbo].[N_Preinscripcion].[ID]

            WHERE
                ([dbo].[N_Preinscripcion].[Periodo] = @prd)

            GROUP BY
                [dbo].[Medios_Pauta].[ID],
                [dbo].[Medios_Pauta].[Nombre]
        ) [Inscritos_Posg]
        ON [Inscritos_Preg].[MedioId] = [Inscritos_Posg].[MedioId]   
GROUP BY 
    [Inscritos_Preg].[MedioId],
    [Inscritos_Preg].[regs1],  
    [Inscritos_Posg].[MedioId],
    [Inscritos_Posg].[regs2]

这是此查询的结果:

MedioId   regs1    regs2    Inscritos
1         8        9        17
4         75       16       91
23        16       NULL     NULL
24        2        NULL     NULL
25        164      127      291
28        8        NULL     NULL
29        3        1        4
30        1        2        3

现在,当我使用COALESCE时,得到的结果与NULL完全相同。这是代码:

SELECT
    [Inscritos_Preg].[MedioId],
    [Inscritos_Preg].[regs1],
    [Inscritos_Posg].[regs2],
    SUM([Inscritos_Preg].[regs1] + [Inscritos_Posg].[regs2])  AS [Inscritos]  
    FROM
        (
            SELECT
                [dbo].[Medios_Pauta].[ID] AS [MedioId],
                COALESCE(COUNT(*), 0) AS [regs1]
            FROM [dbo].[N_Preinscripcion]

            INNER JOIN
                [dbo].[Medios_Pauta]
                ON [dbo].[Medios_Pauta].[ID] = [dbo].[N_Preinscripcion].[MedioId] 

            INNER JOIN
                [dbo].[N_Insc_Preg_Control] 
                ON [dbo].[N_Insc_Preg_Control].[IdPre] = [dbo].[N_Preinscripcion].[ID]

            WHERE
                ([dbo].[N_Preinscripcion].[Periodo] = @prd)

            GROUP BY
                [dbo].[Medios_Pauta].[ID],
                [dbo].[Medios_Pauta].[Nombre]
        ) [Inscritos_Preg]
        LEFT OUTER JOIN
        (
            SELECT
                [dbo].[Medios_Pauta].[ID] AS [MedioId],
                COALESCE(COUNT(*), 0) AS [regs2]
            FROM [dbo].[N_Preinscripcion]

            INNER JOIN
                [dbo].[Medios_Pauta]
                ON [dbo].[Medios_Pauta].[ID] = [dbo].[N_Preinscripcion].[MedioId] 

            INNER JOIN
                [dbo].[N_Insc_Posg_Control] 
                ON [dbo].[N_Insc_Posg_Control].[IdPre] = [dbo].[N_Preinscripcion].[ID]

            WHERE
                ([dbo].[N_Preinscripcion].[Periodo] = @prd)

            GROUP BY
                [dbo].[Medios_Pauta].[ID],
                [dbo].[Medios_Pauta].[Nombre]
        ) [Inscritos_Posg]
        ON [Inscritos_Preg].[MedioId] = [Inscritos_Posg].[MedioId]   
GROUP BY 
    [Inscritos_Preg].[MedioId],
    [Inscritos_Preg].[regs1],  
    [Inscritos_Posg].[MedioId],
    [Inscritos_Posg].[regs2]

结果:

MedioId   regs1    regs2    Inscritos
1         8        9        17
4         75       16       91
23        16       NULL     NULL
24        2        NULL     NULL
25        164      127      291
28        8        NULL     NULL
29        3        1        4
30        1        2        3

如何将这些NULL替换为零(0)

谢谢!

2 个答案:

答案 0 :(得分:2)

我认为您只需要在外部查询中使用COALESCE()

SELECT
    [Inscritos_Preg].[MedioId],
    [Inscritos_Preg].[regs1],
    [Inscritos_Posg].[regs2],
    SUM(COALESCE([Inscritos_Preg].[regs1], 0) + COALESCE([Inscritos_Posg].[regs2], 0))  AS [Inscritos]  

请注意,COUNT()永远不会返回NULL,因此COALESCE()永远不需要COUNT()

答案 1 :(得分:2)

使用COALESCE()函数返回第一个非null值,因此,如果您的列值为null,则它将返回0

SELECT
    [Inscritos_Preg].[MedioId],
    [Inscritos_Preg].[regs1],
   COALESCE ( [Inscritos_Posg].[regs2],0) as regs2,
    SUM(COALESCE ([Inscritos_Preg].[regs1],0) +
    COALESCE ([Inscritos_Posg].[regs2],0)  AS [Inscritos]  
    FROM
        (
            SELECT
                [dbo].[Medios_Pauta].[ID] AS [MedioId],
                COALESCE(COUNT(*), 0) AS [regs1]
            FROM [dbo].[N_Preinscripcion]

            INNER JOIN
                [dbo].[Medios_Pauta]
                ON [dbo].[Medios_Pauta].[ID] = [dbo].[N_Preinscripcion].[MedioId] 

            INNER JOIN
                [dbo].[N_Insc_Preg_Control] 
                ON [dbo].[N_Insc_Preg_Control].[IdPre] = [dbo].[N_Preinscripcion].[ID]

            WHERE
                ([dbo].[N_Preinscripcion].[Periodo] = @prd)

            GROUP BY
                [dbo].[Medios_Pauta].[ID],
                [dbo].[Medios_Pauta].[Nombre]
        ) [Inscritos_Preg]
        LEFT OUTER JOIN
        (
            SELECT
                [dbo].[Medios_Pauta].[ID] AS [MedioId],
                COALESCE(COUNT(*), 0) AS [regs2]
            FROM [dbo].[N_Preinscripcion]

            INNER JOIN
                [dbo].[Medios_Pauta]
                ON [dbo].[Medios_Pauta].[ID] = [dbo].[N_Preinscripcion].[MedioId] 

            INNER JOIN
                [dbo].[N_Insc_Posg_Control] 
                ON [dbo].[N_Insc_Posg_Control].[IdPre] = [dbo].[N_Preinscripcion].[ID]

            WHERE
                ([dbo].[N_Preinscripcion].[Periodo] = @prd)

            GROUP BY
                [dbo].[Medios_Pauta].[ID],
                [dbo].[Medios_Pauta].[Nombre]
        ) [Inscritos_Posg]
        ON [Inscritos_Preg].[MedioId] = [Inscritos_Posg].[MedioId]   
GROUP BY 
    [Inscritos_Preg].[MedioId],
    [Inscritos_Preg].[regs1],  
    [Inscritos_Posg].[MedioId],
    [Inscritos_Posg].[regs2]