Question : Trimmed Average by Month

Hello experts,

I am writing some reporting functions for my application and recently recieved a request to show both a monthly average and a monthly "trimmed" average (eliminating the top and bottom X outliers).

Right now, I have a script to return averages by month, and another script to return an overall trimmed mean, but I am unsure how to go about getting a trimmed average by month.

---------------------------------------------------------------
To get the monthly averages:

SELECT     CONVERT(CHAR(7), job_date, 111) AS Month, AVG(revenue) AS Average
FROM         joblog
GROUP BY CONVERT(CHAR(7), job_date, 111)
ORDER BY CONVERT(CHAR(7), job_date, 111)
---------------------------------------------------------------
To get the (overall) trimmed mean [outliers are the top 5% of values in this case]:

DECLARE @pp float
SET @pp = .05

SELECT @pp AS factor, AVG(revenue) AS TrimmedAverage
FROM @joblog a
WHERE
      (SELECT COUNT(*) FROM @joblog aa WHERE aa.revenue <= a.revenue) >= (SELECT @pp*COUNT(*) FROM @joblog) AND
      (SELECT COUNT(*) FROM @joblog bb WHERE bb.revenue >= a.revenue) >= (SELECT @pp*COUNT(*) FROM @joblog)
---------------------------------------------------------------

What I am having trouble doing is combining these queries to produce a trimmed average by month.  Because this question is extremely vexing, I will award 500 points to anyone who can help me combine these functions.  

Can anyone help me to combine these queries?  I would like the query to return both the Trimmed Average (by month) and the standard Average (by month).

Thank you

Answer : Trimmed Average by Month

Select a.Avg(revenue) as TrimmedAverage, a.month
From (Select revenue, Month(job_date) as month
from @joblog
Where (SELECT COUNT(*) FROM @joblog aa WHERE aa.revenue <= a.revenue) >= (SELECT @pp*COUNT(*) FROM @joblog) AND
     (SELECT COUNT(*) FROM @joblog bb WHERE bb.revenue >= a.revenue) >= (SELECT @pp*COUNT(*) FROM @joblog)) a
group by a.month

how about that?
Random Solutions  
 
programming4us programming4us