Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get sum null values as zero while using group by [duplicate]

Tags:

mysql

I have mysql query

SELECT date, COALESCE(SUM(events),0) AS hotel_c
FROM event_cohort_report
WHERE date >= '2015-08-05' AND campaign='Pointific_Incent' AND country='IN'
GROUP BY date

when i run this query i get this result

date       hotel_c
2015-08-07 5411
2015-08-08 4602
2015-08-09 5151
2015-08-10 183
2015-08-11 1

But i want for all date sum with zero if its not present in the table something like this.

date       hotel_c
2015-08-05 0
2015-08-06 0
2015-08-07 5411
2015-08-08 4602
2015-08-09 5151
2015-08-10 183
2015-08-11 1
like image 368
tejesh s Avatar asked Apr 02 '26 18:04

tejesh s


1 Answers

The code that you are using is correct for getting value for null records. But I don't know the table and records in that as you have not mentioned. I have a working modal on this link

You can also use IFNULL(SUM(column),0) for replacing null with 0

like image 74
Senthil Kumar J Avatar answered Apr 04 '26 07:04

Senthil Kumar J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!