vendredi 15 juillet 2016

sql check for no result in group by clause

I have a sql query for mysql

select sum(quantity), Hour(posted)
from orders
where posted between '05-10-2014' and '05-10-2014' // my timestamp here
group by Hour(posted)

result may be

sum, hour
10, 0
12, 1
13, 3 // note 2 missing in hours
13, 5 // note hour 4 is missing

what I need is

sum,hour
10,0
12,1
0, 2 // 0 for missing hour ( where no record was found in that hour)
13,3 
0, 4 // 0 for missing hour ( where no record was found in that hour)
13,5 

how can I do it?? any help is appreciated

Aucun commentaire:

Enregistrer un commentaire