title: sql sales data grouped by year in separate columns in postgresql
body: <p>I have two input tables:</p>
  <ul>
  <li>analysis (an_id, an_name, an_cost, an_price, an_group)</li>
  <li>orders (ord_id, ord_datetime, ord_an) # orders of analysis (sales)</li>
  </ul>
  <p>For every analysis_id I need to show the amount of orders for years 2020 and 2019.</p>
  <p>Expected output:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>an</th>
  <th>year2019</th>
  <th>year2020</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>32</td>
  <td>41</td>
  </tr>
  <tr>
  <td>2</td>
  <td>29</td>
  <td>28</td>
  </tr>
  <tr>
  <td>3</td>
  <td>42</td>
  <td>37</td>
  </tr>
  <tr>
  <td>4</td>
  <td>26</td>
  <td>35</td>
  </tr>
  <tr>
  <td>5</td>
  <td>32</td>
  <td>34</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>logic in my query:</p>
  <ul>
  <li>step1 - get orders table data only for years 2019,2020 - use CTE and extract()</li>
  <li>step2 - aggregate by year</li>
  </ul>
  <p>My query:</p>
  <pre><code>WITH helper AS (
      SELECT an_id, 
             ord_id, 
             EXTRACT(year from ord_datetime) as year
      FROM analysis a 
      INNER JOIN orders o ON o.ord_an = a.an_id
      WHERE EXTRACT(year FROM ord_datetime) in (2019.0,2020.0)
  )
  SELECT an_id, 
         CASE WHEN year = 2019.0 THEN COUNT(ord_id) ELSE 0 END AS year2019,
         CASE WHEN year = 2020.0 THEN COUNT(ord_id) ELSE 0 END AS year2020
  FROM helper  
  GROUP BY year, an_id
  ORDER BY an_id
  </code></pre>
  <p>The current output of my query:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>an_id</th>
  <th>year2019</th>
  <th>year2020</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>32</td>
  <td>0</td>
  </tr>
  <tr>
  <td>1</td>
  <td>0</td>
  <td>41</td>
  </tr>
  <tr>
  <td>2</td>
  <td>29</td>
  <td>0</td>
  </tr>
  <tr>
  <td>2</td>
  <td>0</td>
  <td>28</td>
  </tr>
  <tr>
  <td>3</td>
  <td>42</td>
  <td>0</td>
  </tr>
  </tbody>
  </table>
  </div>