I have a problem when data is null and the warning has appear when the result is display. How to solve this problem?. How to change the null data to 0 when no data in the table?.
This is my code:-
SELECT DISTINCT c.username AS assigner_officer,
d.description AS ticketcategory,
(SELECT Count(closed)
FROM ticket
WHERE assigned_to = c.user_id
AND closed IS NOT NULL
GROUP BY assigned_to)closedcases,
(SELECT Count(closed)
FROM ticket
WHERE assigned_to = c.user_id
AND closed IS NULL
GROUP BY assigned_to)opencases
FROM ticket a
JOIN ticketlog b
ON a.ticketid = b.ticketid
JOIN access c
ON a.assigned_to = c.user_id
JOIN ticket_category d
ON a.cat_code = d.id
JOIN lookup_department e
ON a.department_code = e.code
The result appear like this:-
Warnings: --->
W (1): Warning: Null value is eliminated by an aggregate or other SET operation.
<---
assigner_officer ticketcategory closedcases opencases
------------------- ----------------- -------------- ------------
abdulhafiz Enquiry (null) 0
affan Enquiry 12 (null)
amirul Enquiry 1 (null)
azrul_fahmi Enquiry 45 0
Azwani Enquiry (null) 0
chai Enquiry 4 (null)
dalinawati Enquiry 1 0
Emmy Complaints (null) 0
Fadhlia Enquiry 38 0
fairulhalif Others 1 (null)
farikh Enquiry (null) 0
ismailh Enquiry 28 0
izzahanna Enquiry (null) 0
Kamsuzilawati Enquiry 1 (null)
-- Warning: Null value is eliminated by an aggregate or other SET operation. As the error says, NULLs are being ignored because we are using aggregate function (SUM, AVG). To avoid the warning we can use “set ansi_warnings off” before the script.
An aggregate function performs a calculation on a set of values, and returns a single value. Except for COUNT(*) , aggregate functions ignore null values. Aggregate functions are often used with the GROUP BY clause of the SELECT statement. All aggregate functions are deterministic.
You would mostly be using COUNT
to summarize over a UID. Therefore
COUNT([uid])
will produce the warning:
Warning: Null value is eliminated by an aggregate or other SET operation.
whilst being used with a left join, where the counted object does not exist.
Using COUNT(*)
in this case would also render incorrect results, as you would then be counting the total number of results (ie parents) that exist.
Using COUNT([uid])
IS a valid way of counting, and the warning is nothing more than a warning. However if you are concerned, and you want to get a true count of uids in this case then you could use:
SUM(CASE WHEN [uid] IS NULL THEN 0 ELSE 1 END) AS [new_count]
This would not add a lot of overheads to your query. (tested mssql 2008)
One way to solve this problem is by turning the warnings off.
SET ANSI_WARNINGS OFF;
GO
Use ISNULL(field, 0)
It can also be used with aggregates:
ISNULL(count(field), 0)
However, you might consider changing count(field) to count(*)
Edit:
try:
closedcases = ISNULL(
(select count(closed) from ticket
where assigned_to = c.user_id and closed is not null
group by assigned_to), 0),
opencases = ISNULL(
(select count(closed) from ticket
where assigned_to = c.user_id and closed is null
group by assigned_to), 0),
You want to put the ISNULL
inside of the COUNT
function, not outside:
Not GOOD: ISNULL(COUNT(field), 0)
GOOD: COUNT(ISNULL(field, 0))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With