I am attempting to find the average of (the top 5% of duration/ per minute).
I wrote this query:
select AVG(Q.duration) Avg5Perc, Q.time FROM
(
select top 5 PERCENT duration, time from table (nolock)
WHERE time between '2015-06-5 13:00' and '2015-06-5 13:59.99'
) Q
group by datepart(minute, Q.time), Q.table
ORDER BY Q.table desc;
Basically, in the sub-select I am acquiring the top 5 percent of values, and in the outer select I am averaging them. However, when I run the query, I do not actually have it grouped by minute.
Any idea? Thanks
Best Answer
Start by working out the duration per minute:
Now get the top 5 percent of this.
Then wrap it up and get the
This then gives you the average of the top five percent. I prefer to use datediff like this because it means you're not restricted to a single hour.