title: SQL statement to get $0 if the customer has no billing from any specific year
body: <p>I am trying to find the average billing amount per year from 2019 to 2021 for every customer, and I want to return $0 if the customer has no billing from any specific year. I tried doing a left join but unfortunately it does not give the result I need. In the table below, how do I return $0 for customer_id 1 since the year for 2021 does not exist. Thanks.</p>
  <p>billing table:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">customer_id</th>
  <th style="text-align: center;">billing_id</th>
  <th style="text-align: center;">created_date</th>
  <th style="text-align: right;">billing_amount</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">id_11</td>
  <td style="text-align: center;">2019-06-21</td>
  <td style="text-align: right;">100</td>
  </tr>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">id_12</td>
  <td style="text-align: center;">2020-05-11</td>
  <td style="text-align: right;">126</td>
  </tr>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">id_13</td>
  <td style="text-align: center;">2019-12-28</td>
  <td style="text-align: right;">86</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">id_21</td>
  <td style="text-align: center;">2019-12-28</td>
  <td style="text-align: right;">28</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">id_22</td>
  <td style="text-align: center;">2020-12-28</td>
  <td style="text-align: right;">56</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">id_23</td>
  <td style="text-align: center;">2021-12-28</td>
  <td style="text-align: right;">26</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Here is my incorrect query:</p>
  <pre><code>Select a.customer_id,
         extract(year from a.created_date),
         avg(a.billing_amount)
  from billing as a
  left join billing as b
      on a.customer_id = b.customer_id 
  where a.created_date between '2019-01-01' and '2021-12-31'
  group by 1, 2
  </code></pre>