title: SQL group by an column but segmented based on another column
body: <p>I have this table which contain roughly more than 100000 rows and with 3 columns:</p>
  
  <ul>
  <li>Account_number</li>
  <li>Report_date</li>
  <li>Outstanding_amount</li>
  </ul>
  
  <p>I need to find a statement that group the outstanding amount by account but also cut it based on the date. Sample data for 1 account:</p>
  
  <pre><code>+----------------+-------------+--------------------+--+
  | account_number | report_date | outstanding_amount |  |
  +----------------+-------------+--------------------+--+
  |              1 | 02/01/2019  |                100 |  |
  |              1 | 03/01/2019  |                100 |  |
  |              1 | 06/01/2019  |                200 |  |
  |              1 | 07/01/2019  |                300 |  |
  |              1 | 10/01/2019  |                200 |  |
  |              1 | 11/01/2019  |                200 |  |
  |              1 | 12/01/2019  |                100 |  |
  +----------------+-------------+--------------------+--+    
  </code></pre>
  
  <p>So if I run this statement: </p>
  
  <p><code>select * from (select account_number, min(report_date) mindate, max(report_date) maxdate, outstading_amount from table1 grouped by account_number, outstanding_amount)</code></p>
  
  <p>The result of this statement should be similar to this:</p>
  
  <pre><code>+----------------+------------+------------+--------------------+
  | account_number |  mindate   |  maxdate   | outstanding_amount |
  +----------------+------------+------------+--------------------+
  |              1 | 02/01/2019 | 12/01/2019 |                100 |
  |              1 | 06/01/2019 | 11/01/2019 |                200 |
  |              1 | 07/01/2019 | 07/01/2019 |                300 |
  +----------------+------------+------------+--------------------+
  </code></pre>
  
  <p>So here I want to separate the result so that the days between mindate and maxdate of one row won't overlap the days in the next row. The result I'm looking is something like this:</p>
  
  <pre><code>+----------------+------------+------------+--------------------+
  | account_number |  mindate   |  maxdate   | outstanding_amount |
  +----------------+------------+------------+--------------------+
  |              1 | 02/01/2019 | 03/01/2019 |                100 |
  |              1 | 06/01/2019 | 06/01/2019 |                200 |
  |              1 | 07/01/2019 | 07/01/2019 |                300 |
  |              1 | 10/01/2019 | 11/01/2019 |                200 |
  |              1 | 12/01/2019 | 12/01/2019 |                100 |
  +----------------+------------+------------+--------------------+
  </code></pre>
  
  <p>Is it possible to construct this statement?</p>