optimization - Optimize MySQL query (generating stats) -
db tables:
i have got mysql query :
select calendar.datefield date, ( select ifnull(sum(visits),0) link_stats link_stats.link_id = '1' , statdate = date ) visits, ( select round(ifnull(sum(leads * (rate/1000)),0),3) link_stats link_stats.link_id = '1' , statdate = date ) cash, ( select ifnull(sum(leads),0) link_stats link_stats.link_id = '1' , statdate = date ) leads `calendar` `calendar`.`datefield` between '2016-05-10' , '2016-05-11'
i try generate stats report per link blank days.
example:
between 11-01-2016 20-01-2016 , link doesn't has records date 12-01-2016.
build table possible dates. call tableofdates , have 1 column: datefield
then rearrange query this:
select d.datefield date, ls.visits, ls.cash, ls.leads tableofdates d left join `calendar` c on c.datefield = d.datefield left join ( select statdate, ifnull(sum(visits),0) visits, round(ifnull(sum(leads * (rate/1000)), 0),3) cash, ifnull(sum(leads),0) leads link_stats ls.link_id = 1 , statdate between '2016-01-11' , '2016-01-20' group statdate ) ls on ls.statdate = d.datefield d.datefield between '2016-01-11' , '2016-01-20'
Comments
Post a Comment