I have a request which count the values of a field in differents cases.
Here is the request :
SELECT SUM(CASE WHEN Reliquat_id = 1 THEN Poids END) AS NbrARRNP,
SUM(CASE WHEN Reliquat_id = 2 THEN Poids END) AS NbrSTNP,
SUM(CASE WHEN Reliquat_id = 3 THEN Nombre END) AS NbrARR,
SUM(CASE WHEN Reliquat_id = 4 THEN Nombre END) AS ST,
SUM(CASE WHEN Reliquat_id = 5 THEN Nombre END) AS NbrCLASS,
SUM(CASE WHEN Reliquat_id = 6 THEN Nombre END) AS NbrINDEX FROM datas WHERE Chantier_id = 4 AND main_id =1;
And sometimes I get a problem if there is no records in a case. The return value is null.
- For example : if there are no records in the case when Reliquat_id = 2 I get null instead of zero.
I see an other question in StackOverflow which is interesting :
How do I get SUM function in MySQL to return '0' if no values are found?
I try to use theses functions to my request but I don't understant the syntax to apply in my case.
Have you an idea ?
Thanks