Alignment is pretty simple: add 0-digit columns, make UNION_ALL , then group and summarize again.
SELECT dt, combination, SUM(v_count) as v_count, SUM(c_count) as c_count FROM ( SELECT DATE(visit.created) as dt, visit.combination as combination, COUNT(visit.id) as v_count, 0 as c_count FROM visit WHERE visit.user_id = 6 AND visit.experiment_id = 1 GROUP BY DATE(visit.created), visit.combination UNION ALL SELECT DATE(conversion.created) as dt, conversion.combination as combination, 0 as v_count, COUNT(conversion.id) as c_count FROM conversion WHERE conversion.user_id = 6 AND conversion.experiment_id = 1 AND conversion.goal_id = 1 GROUP BY DATE(conversion.created), conversion.combination ) as t GROUP BY dt, combination
Now, to accomplish everything. In a more advanced DBMS, this is called a "window" or "analytical" function. For example, in Oracle you can do this:
SELECT dt, combination, SUM(v_count) OVER (PARTITION BY combination ORDER BY dt) as v_cumulative
for the above request and it will give you what you want. However, MySQL does not have such functions. There are methods described here and here , but they are quite complex.
Timekiller
source share