title: mysql join(one to many) aggregate function duplicates rows count
body: <p>I have got three table</p>
  
  <p><strong>book</strong>
  book_id
  outstanding_amt
  created_date</p>
  
  <p><strong>log</strong>
  log_id
  book_id
  paid_id
  event_name (3 types of events)</p>
  
  <p><strong>paid</strong>
  paid_id
  amt_paid</p>
  
  <p>relation between book book and log is one to many. and relation between log and paid one to one.</p>
  
  <p>I want to join these three tables and get data grouped by events with its outstanding_amt.</p>
  
  <p>for eg. 
  event_name   outstanding_amt  amt_paid</p>
  
  <p>The query i am using right now is duplicating outstanding amount.</p>
  
  <pre><code>select event_name, sum(outstanding_amt), sum(amt_paid)
  from log
  inner join book on book.book_id = log.book_id
  inner join paid on log.paid_id = paid.paid_id
  group by month(created_date);
  </code></pre>
  
  <p>since for one row in book, there is multiple rows in log, when i write sum(outstanding_amt), since join is executed before aggregation, these two tables are joined, and after that sum is applied, so sums up duplicate rows that is found in event_log. And it shows much higher amt it actually is.</p>
  
  <p>I have to display data on month basis, when book was created i.e.created_date</p>