title: Count of days in a period
body: <p>I have list of items that have start and end date. Items belong to user. For one item the period can range from 1-5 years. I want to find the count of days that are between the given date range which I would pass from query. Period start is always <code>sysdate</code> and end <code>sysdate - 5 years</code></p>
  
  <p>The count of days returned must also be in the period range.</p>
  
  <p>Example:</p>
  
  <p>I initiate a query as of 15.05.2015) as me being user, so I need to find all days <strong>between 15.05.2010 and 15.05.2015</strong></p>
  
  <p>During that period 2 items have belong to me:</p>
  
  <p>Item 1) 01.01.2010 - 31.12.2010. Valid range: 15.05.2010 - 31.12.2010 = ~195 days</p>
  
  <p>Item 2) 01.01.2015 - 31.12.2015. Valid range: 01.01.2015 - 15.05.2015 = ~170 days</p>
  
  <p>I need a sum of these days that are exactly in that period.</p>
  
  <p>For query right now I just have the count which takes the full range of an item (making it simple):</p>
  
  <pre><code>SELECT SUM(i.end_date - i.start_date) AS total_days 
  FROM items i 
  WHERE i.start_date &gt;= to_date('2010-15-05', 'yyyy-mm-dd')
  AND i.end_date &lt;= to_date('2015-15-05', 'yyyy-mm-dd')
  AND i.user = 'me'
  </code></pre>
  
  <p>So right now this would give me about count of 2 year period dates which is wrong, how should I update my select sum to include the dates that are in the period? Correct result would be 195 + 170. Currently I would get like 365 + 365 or something.</p>