sql - adding missing dates in mysql data set -
i have mysql query this....
select cast(created_at date) 'created_date', dayname(cast(created_at date)) 'day', sum(order_type_id=1) 'pickup', sum(order_type_id=2) 'delivery' orders created_at >= curdate() - interval 7 day , created_at < curdate() , order_status_id != 3 group cast(created_at date);
which returns data below
date | day | pickup | delivery ----------------------------------------------- 2017-08-08 | tuesday | 02 | 01 2017-08-09 | wednesday | 01 | 01 2017-08-10 | thursday | 01 | 00 2017-08-11 | friday | 01 | 01 2017-08-13 | sunday | 01 | 00 2017-08-14 | monday | 01 | 01
what i'm trying summery of delivery orders , pickup orders past 7 days excluding today.
my prob : if closely observe above, you'll notice dont have output 2017-08-12 (saturday) since there no operations done in same date. idea how these empty dates output have below????
date | day | pickup | delivery ----------------------------------------------- 2017-08-08 | tuesday | 02 | 01 2017-08-09 | wednesday | 01 | 01 2017-08-10 | thursday | 01 | 00 2017-08-11 | friday | 01 | 01 2017-08-12 | saturday | 00 | 00 2017-08-13 | sunday | 01 | 00 2017-08-14 | monday | 01 | 01
if have data on days (just not status want on date), can use conditional aggregation:
select cast(created_at date) created_date, dayname(cast(created_at date)) day, sum(order_type_id = 1 , order_status_id <> 3) pickup, sum(order_type_id = 2 , order_status_id <> 3) delivery orders created_at >= curdate() - interval 7 day , created_at < curdate() group cast(created_at date);
if not case, need table 1 row per date want. simplest method calendar table.
Comments
Post a Comment