I have this SQL query:
var sqlQuery =
"SELECT '"+dateRanges[ic].DATE_FROM+"' as DATE_FROM, "+
" '"+dateRanges[ic].DATE_TO+"' as DATE_TO, "+
" COUNT(*) AS DIALS_CNT, "+
" SUM(CASE WHEN dc.call_result = '"+CALL_RESULT_STATE_APPT+"' THEN 1 ELSE 0 END) AS '"+APPT_CNT+"', "+
" SUM(CASE WHEN dc.call_result = '"+CALL_RESULT_STATE_CONV_NO_APPT+"' THEN 1 ELSE 0 END) AS '"+CONVERS_CNT+"' , "+
" SUM(CASE WHEN dc.call_result = '"+CALL_RESULT_STATE_CANNOT_REACH+"' THEN 1 ELSE 0 END) AS '"+CANNOT_REACH_CNT+"' "+
" FROM "+DIALED_CALLS_TABLE+" dc "+
" WHERE dc.date BETWEEN '"+dateRanges[ic].DATE_FROM+"' AND '"+dateRanges[ic].DATE_TO+"';";
Problem is that if the sum of the values is zero result is containing value null.
I thought that i can solve it using THEN 1 ELSE 0 END
but it is not working.
How can i solve it please to get result with zeros instead of the null?
Thanks for any help.
I have this SQL query:
var sqlQuery =
"SELECT '"+dateRanges[ic].DATE_FROM+"' as DATE_FROM, "+
" '"+dateRanges[ic].DATE_TO+"' as DATE_TO, "+
" COUNT(*) AS DIALS_CNT, "+
" SUM(CASE WHEN dc.call_result = '"+CALL_RESULT_STATE_APPT+"' THEN 1 ELSE 0 END) AS '"+APPT_CNT+"', "+
" SUM(CASE WHEN dc.call_result = '"+CALL_RESULT_STATE_CONV_NO_APPT+"' THEN 1 ELSE 0 END) AS '"+CONVERS_CNT+"' , "+
" SUM(CASE WHEN dc.call_result = '"+CALL_RESULT_STATE_CANNOT_REACH+"' THEN 1 ELSE 0 END) AS '"+CANNOT_REACH_CNT+"' "+
" FROM "+DIALED_CALLS_TABLE+" dc "+
" WHERE dc.date BETWEEN '"+dateRanges[ic].DATE_FROM+"' AND '"+dateRanges[ic].DATE_TO+"';";
Problem is that if the sum of the values is zero result is containing value null.
I thought that i can solve it using THEN 1 ELSE 0 END
but it is not working.
How can i solve it please to get result with zeros instead of the null?
Thanks for any help.
SUM returns NULL when it does not get any value to begin with, i.e., when your WHERE clause does not match any rows.
If you can live with the result being a floating-point number, replace SUM with TOTAL. Otherwise, you can put an IFNULL around the entire sum:
SELECT ... IFNULL(SUM(CASE ... END), 0) FROM ...
I think if some of your initiall values are null even the case/else won't work (null neither equals nor not-equals anything). Try putting some default, blank type value for nulls using IFNULL.
...
" SUM(CASE WHEN IFNULL(dc.call_result,some_default_value) = '"+CALL_RESULT_STATE_APPT+"' THEN 1 ELSE 0 END) AS '"+APPT_CNT+"', "+
...