sql - MySQL Date Range Multi Column Count and Group By Select Statement -
i have query have been working on while cannot seem down. other answers on here work counting amount date range grouping date count. need have 2 columns counted , grouped date.
for example here query have tried work:
(select count(*) arrived, date(arrived) date, 'arrived' source products arrived between '2016-01-01' , '2016-01-31' group date(date) order date asc) union (select count(*) released, date(released) date, 'released' source products released between '2016-01-01' , '2016-01-31' group date(date) order date asc)
however returns following:
arrived date source 3 2016-01-12 arrived 2 2016-01-28 arrived 1 2016-01-29 arrived 1 2016-01-05 released
what requiring this:
date arrived released 2016-01-05 0 1 2016-01-12 3 0 2016-01-28 2 0 2016-01-29 1 0
any suggestions? thank you.
you can apply conditional aggregation derived table obtained union all
operation 'arrived' , 'released' dates:
select `date`, count(case when type = 'arrived' 1 end) arrived, count(case when type = 'released' 1 end) released ( select arrived `date`, 'arrived' type products arrived between '2016-01-01' , '2016-01-31' union select released `date`, 'released' type products released between '2016-01-01' , '2016-01-31') t group `date`
Comments
Post a Comment