Calculate Growth rate
Explanation

First all the JOINing is done.  This inflates the number of rows.  Then you do a GROUP BY and SUM.  Hence, the "unbelievably large numbers".

Solution 1

A solution is usually to do the GROUP BY and SUM over just one table.  If that can't be done, then find the ids for that one table, dedup them (via DISTINCT or GROUP BY), then do the grouping and summing.

Give this a try...


Rip out all mention of tables other than a.  See if the grouping and summing works correctly.
Make that a subquery and put it as a 'table' in with the rest of the tables:

SELECT ...
    FROM ( SELECT ... SUM... FROM profit_log WHERE ... GROUP BY ... ) AS a
    JOIN b ... -- as you have the rest


Solution 2

Another approach is to use subqueries for the other stuff...

SELECT a.`market_id`,
    SUM(a.`total_purchase`) as acc_purchase,
    SUM(a.`total_profit`) as acc_profit,
    SUM(a.`company_profit`) as acc_company_profit,
    ( SELECT ((b.`total_purchase` - c.`total_purchase`)/ c.`total_purchase`)
        FROM `profit_log` as b on a.`market_id` = b.`market_id`
    ) * 100 as growth,
    ...
FROM `profit_log` as a
WHERE a.`date` &gt;= '2016-05-01'
  AND a.`date` &lt; ('2016-05-05' + INTERVAL 1 DAY)
  AND a.`market_id` IN (0,20)
GROUP BY a.`market_id`


Need indexes

InnoDB tables really need a PRIMARY KEY.

Also you need this; it should increase performance significantly:  INDEX(market_id, date)