Quartile/Percentile 在 MS Access 中通过 SQL 使用 GROUP BY 当某些值可以为 NULL 时

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

我想为可以为 NULL 的字段计算子组的百分位数。字段 IU 为 1 或 Null。具体来说:

*my table: tblFirst250
*group by: IU = 1 (which is Nullable)
*percentile of: GM (which is Nullable)

我从以下开始(但我愿意接受更好的方法):

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

直接取自此处:http://blogannath.blogspot.com/2010/03/microsoft-access-tips-tricks-statistics.html#ixzz3dEf9ZJSq

到目前为止我有这个:

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;

产生: IU , 1 ;25Percentile -0.706278906030414, -0.706278906030414

...这看起来像是分配给所有事物的所有事物的四分位数。

Issues/questions/requests/notes:

  1. 我想要以下值是 IU = 1 的四分位数 25: 国际单位 1; 25 个百分点 -0.706278906030414
  2. 查询速度很慢。
  3. 我认为是子查询让我感到困惑。

底部附近缺少一个 WHERE 子句:

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;