I am looking to calculate a percentile of a subgroup for a field that can be NULL. Field IU is either 1 or Null. Specifically:
*my table: tblFirst250
*group by: IU = 1 (which is Nullable)
*percentile of: GM (which is Nullable)
I am starting with the following (but I am open to better ways of doing this):
select T.groupField, 0.75*(select max(myField) from myTable where
myTable.myField in (select top 25 percent myField from myTable where
myTable.groupField = T.groupField order by myField)) + 0.25*(select
min(myField) from myTable where myTable.myField in (select top 75 percent
myField from myTable where myTable.groupField = T.groupField order by
myField desc)) AS 25Percentile from myTable AS T group by T.groupField
Taken directly from here: http://blogannath.blogspot.com/2010/03/microsoft-access-tips-tricks-statistics.html#ixzz3dEf9ZJSq
So far I have this:
SELECT T.IU, 0.75*(SELECT Max(GM) FROM tblFirst250 WHERE tblFirst250.GM IN
(SELECT TOP 25 PERCENT GM FROM tblFirst250
WHERE tblFirst250.IU = 1 AND GM Is Not Null ORDER BY GM)) + 0.25*
(SELECT Min(GM) FROM tblFirst250 WHERE tblFirst250.GM IN
(SELECT TOP 75 PERCENT GM FROM tblFirst250
WHERE tblFirst250.IU = 1 AND GM Is Not Null ORDER BY GM desc)) AS 25Percentile
FROM tblFirst250 AS T
GROUP BY T.IU;
Which yields: IU , 1 ;25Percentile -0.706278906030414, -0.706278906030414
...which looks like the quartile of everything assigned to everything.
Issues/questions/requests/notes:
- I would like the following where the value is the quartile 25 where IU = 1: IU 1; 25Percentile -0.706278906030414
- The query is pretty slow.
- It's the sub queries that are tripping me up I think.
Just a missing WHERE clause near the bottom: