Is there a way to modify this query to return ‘0’ in the Grade column if no records are found? Right now when nothing is found I get a blank row.
SELECT COALESCE(Score, 0) AS Score, COALESCE(Grade,0) AS Grade, LAST_UPDATE, '' as dummy
FROM (
SELECT (CASE ScanName WHEN '%=APP%' THEN '= Network System' END) AS System,
(sum(CASE severity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN 3 THEN 6 WHEN 4 THEN 3
END)/COUNT(severity )) AS Score,
(sum(CASE severity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN 3 THEN 6 WHEN 4 THEN 3
END)/COUNT(severity )) AS Grade, LAST_UPDATE
FROM missing_internal
WHERE ScanName like '%APP%'
and LAST_UPDATE >= SUBDATE((SELECT MAX(LAST_UPDATE) from missing_internal), INTERVAL 1 day)
HAVING System IS NOT NULL
LIMIT 1
UNION ALL
SELECT NULL AS Score, NULL AS Grade, 0 AS LAST_UPDATE, '' as dummy
ORDER BY LAST_UPDATE DESC
) as T2
LIMIT 1;
Score and Grade are the same on purpose as I am using a separate script to apply a letter grade to the score.
Take a look at the COALESCE function …
If i well understood your question the below query should do exactly what you want(getting zeros when now rows are returned) :
I have not tested the query, so you could need to arrange it … However take the idea …
If you want to get zero only when a column return null result, you can use COALESCE in the same way as above example :