title: How to combine duplicated rows into a single row?
body: <p>I have a table with each row representing a person. In this table there are a lot of duplicates that I want to get rid of. I want to deduplicate based on <code>name</code> and <code>age</code> only.  However, the information in columns can be spread between different rows for the same employee. For example:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>name</th>
  <th>age</th>
  <th>height</th>
  <th>eye_color</th>
  <th>weight</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>John</td>
  <td>32</td>
  <td>null</td>
  <td>green</td>
  <td>null</td>
  </tr>
  <tr>
  <td>John</td>
  <td>32</td>
  <td>null</td>
  <td>null</td>
  <td>75</td>
  </tr>
  <tr>
  <td>John</td>
  <td>32</td>
  <td>180</td>
  <td>null</td>
  <td>null</td>
  </tr>
  <tr>
  <td>John</td>
  <td>32</td>
  <td>null</td>
  <td>null</td>
  <td>74</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>In this example, the expected output would be:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>name</th>
  <th>age</th>
  <th>height</th>
  <th>eye_color</th>
  <th>weight</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>John</td>
  <td>32</td>
  <td>180</td>
  <td>green</td>
  <td>75</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Note that it doesn't matter if the weight is 75 or 74, the order is not important for my use case, I just want to fill as much <code>null</code> as possible, while removing duplicates.</p>
  <p>There is a unicity constraint on some columns, so simply updating all rows with the desired values and then keeping one row per group is not an option unfortunately, ie updating the table to look like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>name</th>
  <th>age</th>
  <th>height</th>
  <th>eye_color</th>
  <th>weight</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>John</td>
  <td>32</td>
  <td>180</td>
  <td>green</td>
  <td>75</td>
  </tr>
  <tr>
  <td>John</td>
  <td>32</td>
  <td>180</td>
  <td>green</td>
  <td>75</td>
  </tr>
  <tr>
  <td>John</td>
  <td>32</td>
  <td>180</td>
  <td>green</td>
  <td>75</td>
  </tr>
  <tr>
  <td>John</td>
  <td>32</td>
  <td>180</td>
  <td>green</td>
  <td>75</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>before deduplicating is not possible.</p>
  <p>If <code>age</code> or <code>name</code> is <code>null</code> for an employee, it shouldn't be deduplicated at all.</p>
  <p>A similar question on stackoverflow was <a href="https://stackoverflow.com/questions/27927251/remove-duplicates-with-less-null-values">Remove duplicates with less null values</a> but they only keep the row with the least amount of <code>null</code> so it's not really solving my problem.</p>
  <p>However, maybe there is something to do with some aggregation as shown in <a href="https://stackoverflow.com/questions/61874745/postgresql-get-first-non-null-value-per-group">PostgreSQL: get first non null value per group</a> but I couldn't get anything to work for the moment.</p>
  <p>Any idea?</p>
  <p>Thanks.</p>