title: How sum values in days intervals MySQL 5.7?
body: <p>I have a server with MySQL 5.7.
  I have two tables. First one <code>t</code> contains creating dates for each id. Second table <code>t0</code> contains profit records day by day for each id.
  I want to get columns with sums of profit for first and second 30 days for each id as well as for the first day.</p>
  <pre><code>SELECT t.created_at,
     t.id,
     sum(t1.profit) profit_1_week,
     sum(t2.profit) profit_2_week,
     sum(t3.profit) profit_1_day
  FROM t
  LEFT JOIN t0 t1 ON t.id = t.id
  AND t1.inport_date BETWEEN t.created_at AND DATE_ADD(t.created_at, INTERVAL 30 DAY)
  LEFT JOIN t0 t2 ON t.id = t.id
  AND t2.inport_date BETWEEN DATE_ADD(t.created_at, INTERVAL 30 DAY) AND DATE_ADD(t.created_at, INTERVAL 60 DAY)
  LEFT JOIN t0 t3 ON t.id = t.id
  AND t3.inport_date BETWEEN t.created_at AND DATE_ADD(t.created_at, INTERVAL 1 DAY)
  GROUP BY t.created_at,
           t.id
  ORDER BY t.created_at
  </code></pre>
  <p>This code runs but sums are wrong because sum of the first day much more then monthly. Where I'm wrong and how to fix it?</p>