title: SQL Server: fill a range with dates from overlapping intervals with priority
body: <p>I need to fill the range from 2017-04-01 to 2017-04-30 with the data from this table, knowing that the highest priority records should prevail over those with lower priorities</p>
  
  <pre><code>id     startValidity  endValidity  priority
  -------------------------------------------
  1004    2017-04-03    2017-04-30    1   
  1005    2017-04-10    2017-04-22    2   
  1010    2017-04-19    2017-04-23    3   
  1006    2017-04-24    2017-04-28    2   
  1008    2017-04-26    2017-04-28    3   
  </code></pre>
  
  <p>In practice I would need to get a result like this:</p>
  
  <pre><code>id      startValidity  endValidity  priority
  --------------------------------------------
  1004    2017-04-03    2017-04-09    1   
  1005    2017-04-10    2017-04-18    2   
  1010    2017-04-19    2017-04-23    3   
  1006    2017-04-24    2017-04-25    2   
  1008    2017-04-26    2017-04-28    3   
  1004    2017-04-29    2017-04-30    1   
  </code></pre>