By looking at your query, <b>I will assume</b> you are doing this on an Oracle database.
You are getting more days because you are calculating the total downtime for each record, even when some records comprise dates outside your desired range.
You will have to check the end time for the records, and when the end_time is greater than the last day you want to include in the report, you should calculate the downtime based on that day, and not on the end_time column. And something similar needs to be done with the start times.
A combination of the DECODE and SIGN functions could help.
The downtime calculation should be something like this:
<pre>sum(decode(sign(a.end_time-to_date(’30-MAY-09′)),1,’30-MAY-2009′,a.end_time) – decode(sign(a.start_time-to_date(’24-MAY-09′)),-1,’24-MAY-2009′,a.start_time))</pre>
Also, filtering the results using the start_time only will not work.
You need a condition similar to this:
<pre>a.start_time <= ’30-MAY-09′ AND a.end_time >= ’24-MAY-09′ </pre>