I need to calculate the median number of days between the oppo_opened
date and the oppo_offered
date on a table called Opportunity
, grouping results by a joined table Company
. Doing the average is very easy:
----Average Opened to Offered last 12 months
Select Comp_Name,AVG(DATEDIFF(day,Oppo_Opened,oppo_offerDate)) as averageDTO from company join Opportunity on oppo_lender = Comp_CompanyId
where DATEADD(Year,-1,GETDATE()) > Oppo_Opened AND oppo_offerDate is not null
group by Comp_Name order by averageDTO desc
The above returns one row per company, as we are grouping by the Comp_Name
.
It's not quite so easy when doing this for a median. I located the following post:
Function to Calculate Median in Sql Server
And that got me to here:
SELECT Comp_Name,
PERCENTILE_CONT(0.5)
WITHIN GROUP (ORDER BY DATEDIFF(day,Oppo_Opened,oppo_offerDate))
OVER (PARTITION BY Comp_Name) AS MedianCont
from company inner join Opportunity on oppo_lender = Comp_CompanyId
where DATEADD(Year,-1,GETDATE()) > Oppo_Opened AND oppo_offerDate is not null
order by MedianCont desc
However, this returns one row per Opportunity
record and not one row per Company
record. I can't do a simple group by Comp_Name
on this either as we then get:
Msg 8120, Level 16, State 1, Line 3
Column 'Opportunity.Oppo_Opened' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 3
Column 'Opportunity.oppo_offerDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
And putting an aggregate method on either of the above I am presuming will screw up my median calculation.
My Question: How can I group by the PERCENTILE_CONT
partition to get one row per Company
(I should get 209 rows) as opposed to one row per Opportunity
(north of 30k rows)?