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 returnnullby the type you want, in this case the0. ex.:COALESCE(SUM(1), 0).– Guilherme Lautert
another alternative would be to make a
castex.:SUM(1)::integer.– Guilherme Lautert
so? (cast(sum (1) as integer(0))
– Mara Pimentel
from what I understand the return is in
SQLexternal, but you are applying this rule in theSQLinternal. I do not know how it is in theSQL server, but in case should not be applying theCOALESCEinPIVOT (COALESCE(SUM(contar),0) FOR Shortname IN ([ACS],[URO])) pvt?– Guilherme Lautert
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'.
– Mara Pimentel