optimization - Optimize MySQL query (generating stats) -


db tables:

enter image description here

enter image description here

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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -