SELECT SUM( CASE WHEN is_half = 1 THEN 1 ELSE 0 END ) halfCount, SUM( CASE
WHEN is_half = 0 THEN 1 ELSE 0 END ) totalCount FROM employee_leave WHERE
YEAR(leave_date_from) = YEAR(CURRENT_DATE()) AND employee_id="EMP12" AND
leave_status=1
With this query I am getting results as(If there are no values):
halfCount | totalCount
NULL | NULL
How can I get 0 instead of NULL?
Desired output:
halfCount | totalCount
0 | 0
Assuming your query is correct, you can use COALESCE to change null into 0 (or any other number)
SELECT COALESCE(SUM( CASE WHEN is_half = 1 THEN 1 ELSE 0 END ), 0) as 'halfCount',
COALESCE(SUM( CASE WHEN is_half = 0 THEN 1 ELSE 0 END ), 0) as 'totalCount'
FROM employee_leave
WHERE YEAR(leave_date_from) = YEAR(CURRENT_DATE()
AND employee_id="EMP12"
AND leave_status=1
I'm not really sure if this is error-free, as I can't test it in my machine, but that is how to use COALESCE
try this
SELECT IFNULL(SUM( CASE WHEN is_half = 1 THEN 1 ELSE 0 END ),0) halfCount, IFNULL(SUM( CASE
WHEN is_half = 0 THEN 1 ELSE 0 END ),0) totalCount FROM employee_leave WHERE
YEAR(leave_date_from) = YEAR(CURRENT_DATE()) AND employee_id="EMP12" AND
leave_status=1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.