title: Sum of working days with date ranges from multiple records (overlapping)
body: <p>suppose there are records as follows:</p>
  
  <pre><code>Employee_id, work_start_date, work_end_date
  
  1, 01-jan-2014, 07-jan-2014
  1, 03-jan-2014, 12-jan-2014
  1, 23-jan-2014, 25-jan-2014
  2, 15-jan-2014, 25-jan-2014
  2, 07-jan-2014, 15-jan-2014
  2, 09-jan-2014, 12-jan-2014
  </code></pre>
  
  <p>The requirement is to write an SQL select statment which would summarize the work days grouped by employee_id, but exclude the overlapped periods (meaning - take them into calculation only once).</p>
  
  <p>The desired output would be:</p>
  
  <pre><code>Employee_id, worked_days
  
  1, 13
  2, 18
  </code></pre>
  
  <p>The calculations for working days in the date range are done like this:
  If work_start_date = 5 and work_end_date = 9 then worked_days = 4 (9 - 5).</p>
  
  <p>I could write a pl/sql function which solves this (manually iterating over the records and doing the calculation), but I'm sure it can be done using SQL for better performance.</p>
  
  <p>Can someone please point me in the right direction?</p>
  
  <p>Thanks!</p>