title: Intersection and consolidation of time periods in SQL
body: <p>I want to achieve similar function that is available in <a href="https://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET" rel="nofollow noreferrer">Time Period Library for .NET</a>, but in SQL.</p>
  
  <p>First, I have a table with several rows with an Start Date and an End Date, and I want to consolidate them together like this:</p>
  
  <p><a href="https://i.stack.imgur.com/uWwv8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uWwv8.png" alt="Combination"></a></p>
  
  <p>Then with that result and another coming from a different table, I want to find out the intersection between the two of them, like this but only 2 inputs (find the periods that are present in both):</p>
  
  <p><a href="https://i.stack.imgur.com/b5dAL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b5dAL.png" alt="Intersection"></a></p>
  
  <p>Once I have the intersection is just summing up the time on it.</p>
  
  <p>Here I provide a SQL Fiddle with the expected output with an example:</p>
  
  <p><a href="http://sqlfiddle.com/#!18/504fa/3" rel="nofollow noreferrer">http://sqlfiddle.com/#!18/504fa/3</a></p>