title: SQL Db2 - How to unify two rows in one using datetime
body: <p>I've got a table where we have registries of employees and where they have worked. In each row, we have the employee's starting date on that place. It's something like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Employee ID</th>
  <th>Name</th>
  <th>Branch</th>
  <th>Start Date</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>John Doe</td>
  <td>234</td>
  <td>2018-01-20</td>
  </tr>
  <tr>
  <td>1</td>
  <td>John Doe</td>
  <td>300</td>
  <td>2019-03-20</td>
  </tr>
  <tr>
  <td>1</td>
  <td>John Doe</td>
  <td>250</td>
  <td>2022-01-19</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Jane Doe</td>
  <td>200</td>
  <td>2019-02-15</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Jane Doe</td>
  <td>234</td>
  <td>2020-05-20</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I need a query where the data returned looks for the next value, making the starting date on the next branch as the end of the current. Eg:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Employee ID</th>
  <th>Name</th>
  <th>Branch</th>
  <th>Start Date</th>
  <th>End Date</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>John Doe</td>
  <td>234</td>
  <td>2018-01-20</td>
  <td>2019-03-20</td>
  </tr>
  <tr>
  <td>1</td>
  <td>John Doe</td>
  <td>300</td>
  <td>2019-03-20</td>
  <td>2022-01-19</td>
  </tr>
  <tr>
  <td>1</td>
  <td>John Doe</td>
  <td>250</td>
  <td>2022-01-19</td>
  <td>---</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Jane Doe</td>
  <td>200</td>
  <td>2019-02-15</td>
  <td>2020-05-20</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Jane Doe</td>
  <td>234</td>
  <td>2020-05-20</td>
  <td>---</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>When there is not another register, we assume that the employee is still working on that branch, so we can leave it blank or put a default &quot;9999-01-01&quot; value.
  Is there any way we can achieve a result like this using only SQL?</p>
  <blockquote>
  <p>Another approach to my problem would be a query that returns only the row that is in a range. For example, if I look for what branch John Doe worked in 2020-12-01, the query should return the row that shows the branch 300.</p>
  </blockquote>