mysql - Monthly Sales comparison by day -
i'm using table store sales day , want way sales each day , compare same day last month, this:
i want number of sales each day like
daya | salesa | dayb | salesb 1 | 3 | 1 | 4 2 | 2 | 2 | 5
and on, since month 7 days rest of days have 0 sales... 1 table sales. this.
id | date 1 | 2015-06-01 2 | 2015-06-02 3 | 2015-06-02 4 | 2015-06-02 30 | 2015-07-01 40 | 2015-07-05
so in day 2015-06-02 had 3 sales... , rest 1 sale.
not sure whats best way can create graph, should use 2 queries or there way 1 ?
it's not 1 query version show current month's sales (in b columns) next last month's sales (in columns), if date had no sales.
create table sales_totals select `date`, count(*) total sales group `date`; select day(datea) daya, salesa, day(dateb) dayb, salesb ( select s1.`date` datea, s1.total salesa, s2.`date` dateb, s2.total salesb sales_totals s1 left join sales_totals s2 on date_add(s1.`date`, interval 1 month) = s2.`date` union select s3.`date` datea, s3.total salesa, s4.`date` dateb, s4.total salesb sales_totals s3 right join sales_totals s4 on s3.`date` = date_sub(s4.`date`, interval 1 month) ) x year(dateb) = year(now()) , month(dateb) = month(now()); drop table sales_totals;
Comments
Post a Comment