title: How to create a pivot table by product by month in SQL
body: <p>I have 3 tables:</p>
  
  <pre><code>users (id, account_balance)
  grocery (user_id, date, amount_paid)
  fishmarket (user_id, date, amount_paid)
  </code></pre>
  
  <p>Both fishmarket and grocery tables may have multiple occurrences for the same user_id with different dates and amounts paid or have nothing at all for any given user. I am trying to develop a pivot table of the following structure:</p>
  
  <pre><code>id | grocery_amount_paid_January | fishmarket_amount_paid_January
    1          10                           NULL
    2          40                           71
  </code></pre>
  
  <p>The only idea I can come with is to create multiple left joins, but this should be wrong since there will be 24 joins (per each month) for each product. Is there a better way?</p>