SQL - set null values to 0

Asked

Viewed 1,948 times

0

I have a table pivot that returns some null values in SUM(1). I wish they were zero instead of zero, but I’m not getting it.

Pivot table original

SELECT
        Description AS Categoria,
        [ACS],
        [URO] 
FROM 
        (
            SELECT 
                    GroupType.Description,
                    Speciality.Shortname,
                    SUM(1) as contar
            FROM
                    DoctorEnterpriseDetails 
                    INNER JOIN Speciality ON DoctorEnterpriseDetails.Speciality1 = Speciality.SpecialityId
                    INNER JOIN GroupType ON DoctorEnterpriseDetails.GroupId = GroupType.GroupId
            WHERE 
                    (DoctorEnterpriseDetails.EnterpriseId = 48)
            Group By 
                    GroupType.Description,
                    Speciality.Shortname
        ) as ps PIVOT (SUM(contar) FOR Shortname IN ([ACS],[URO])) pvt
ORDER BY 
    description

and this was the one I did with the case when to try to put the nulls to zero

SELECT 
        Description AS Categoria,
        [ACS],
        [URO]
FROM
        (
          SELECT 
                  GroupType.Description,
                  Speciality.Shortname,
                  GroupType.GroupId,
                  (CASE WHEN (SUM(1) IS NULL) THEN 0 ELSE SUM(1) END) AS contar
          FROM 
                  DoctorEnterpriseDetails
                  INNER JOIN Speciality ON DoctorEnterpriseDetails.Speciality1 = Speciality.SpecialityId
                  INNER JOIN GroupType ON DoctorEnterpriseDetails.GroupId = GroupType.GroupId
          WHERE 
                  (DoctorEnterpriseDetails.EnterpriseId = 48)
          GROUP BY 
                  GroupType.Description,
                  Speciality.Shortname,
                  DoctorEnterpriseDetails.GroupId,
                  GroupType.GroupId
        ) 
        AS ps PIVOT (SUM(contar) FOR Shortname IN ([ACS],[URO])) pvt
ORDER BY 
        GroupId;

I have tried with the coalesce and it does the same. I can’t figure out what’s wrong with it. Thank you

  • Check that the bank you are using has the function COALESCE, she basically replaced a return null by the type you want, in this case the 0. ex.: COALESCE(SUM(1), 0).

  • another alternative would be to make a cast ex.: SUM(1)::integer.

  • so? (cast(sum (1) as integer(0))

  • from what I understand the return is in SQL external, but you are applying this rule in the SQL internal. I do not know how it is in the SQL server, but in case should not be applying the COALESCE in PIVOT (COALESCE(SUM(contar),0) FOR Shortname IN ([ACS],[URO])) pvt ?

  • Group By Grouptype.Description, Speciality.Shortname) as ps PIVOT (COALESCE(SUM(count),0) FOR Shortname IN ([ACS],[URO])) Pvt ORDER BY Description ... had already thought about it but gives me Incorrect syntax near the keyword 'COALESCE'.

2 answers

1

Something prevents the use of COUNT(*) in place of SUM(1)? COUNT(), unlike SUM(), returns zero when the set over which the function is operating is empty (and especially in this case, makes it much clearer to whoever is reading the code what is being done).

1

It has an SQL function called Coalesce. When a value is null it returns the value of the parameter you determine in it.

Or you can use the function Isnull.

Select Coalesce(Valor, 0) As Valor From Tabela
Select ISNull(Sum(Valor),0) As Valor From Tabela

https://msdn.microsoft.com/pt-br/library/ms190349.aspx

Then use the Coalesce in your Soma field and see if you do what you want.

  • do assime devolve me null à mesma Coalesce(SUM(1), 0) As contar

  • I edited my answer. Try using Ifnull.

  • did not know but give me this 'Ifnull' is not a recognized built-in Function name.

  • How you’re using the function, we can see ?

  • SELECT Description AS Categoria, [ACS],[URO] FROM (SELECT Grouptype.Description, Speciality.Shortname, Grouptype.Groupid, Ifnull(Sum(1),0) As contar

  • I edited my answer. I changed the function from Ifnull to Isnull. I forgot it was Sql Server. Test now.

  • SELECT Description AS Category, [ACS],[URO] FROM (SELECT Grouptype.Description, Speciality.Shortname, Grouptype.Groupid, Isnull(Sum(1),0) count them ... and return to me null lol

  • 1

    @Marapimentel I think you want one SUM(COALESCE(contar, 0)).

  • 1

    already managed to solve, it was not easy but I had to make changes to my code. Thanks for the help!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.