Quartile/Percentile in MS Access via SQL with a GROUP BY when some values can be NULL

别来无恙 提交于 2021-02-02 09:53:50

问题


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:

  1. I would like the following where the value is the quartile 25 where IU = 1: IU 1; 25Percentile -0.706278906030414
  2. The query is pretty slow.
  3. It's the sub queries that are tripping me up I think.

回答1:


Just a missing WHERE clause near the bottom:

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
WHERE T.IU = 1
GROUP BY T.IU;


来源:https://stackoverflow.com/questions/30872060/quartile-percentile-in-ms-access-via-sql-with-a-group-by-when-some-values-can-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!