title: Merge/Combine continuous date records using loop in SQL
body: <p>I have a requirement where I need to merge records which are continuous and belong to a certain type of group and few other conditions.</p>
  
  <ol>
  <li>List item</li>
  </ol>
  
  <p>To combine records  -we should consider it as continuous only if it start as <strong>Main group</strong>, so the 1st records in here should be treated as individual even thought it is continuous</p>
  
  <ol start="2">
  <li>List item</li>
  </ol>
  
  <p>Do not Combine if it does not end as Main group,</p>
  
  <ol start="3">
  <li>List item</li>
  </ol>
  
  <p>If there is gap of a day then don't combine</p>
  
  <pre><code>MemberName   Group  StartDate      EndDAte
  Priya         Sub   01/01/2017     01/20/2017 --Do not merge,not started in Main
  Priya         Main  01/20/2017     01/30/2017 --merge
  Priya         Sub   01/30/2017     02/05/2017 --merge
  Priya         Main  02/06/2017     02/06/2017 --Do not merge,Break
  Priya         sub   02/07/2017     02/15/2017 --Do not merge,not started in Main
  Priya         Main  02/15/2017     02/20/2017 --Merge
  Priya         Main  02/20/2017     02/25/2017 --Do not merge,Break
  Priya         sub   02/28/2017     02/28/2017 --Do not merge,break also did not end as Main
  </code></pre>
  
  <p>output should look like:</p>
  
  <pre><code>MemberName   Group  StartDate      EndDAte
  Priya        Sub    01/01/2017     01/20/2017 
  Priya        Main   01/20/2017     02/05/2017
  Priya        Main   02/06/2017     02/06/2017
  Priya        sub    02/07/2017     02/15/2017
  Priya        Main   02/15/2017     02/25/2017
  Priya        sub    02/28/2017     02/28/2017 
  </code></pre>
  
  <p>I have to use a If-while loop here.</p>