以下是使用VBA在Access中计算数据的中位数的代码示例:
首先,将数据表中的数据按两列分组,并将中位数计算为新列:
SELECT Table1.ColumnA, Table1.ColumnB,
IIF(COUNT() Mod 2 = 0,
AVG(DLookUp("[DataColumn]", "[Table1]", "[ColumnA] = '" & [Table1].[ColumnA] & "'
AND [ColumnB] = '" & [Table1].[ColumnB] & "' AND " & _
"[DataColumn] NOT IN (SELECT TOP " & (COUNT() / 2) _
& " [DataColumn] FROM [Table1] WHERE [ColumnA] = '"_
& [Table1].[ColumnA] & "' AND [ColumnB] = '"_
& [Table1].[ColumnB] & "' ORDER BY [DataColumn] ASC)) _
- DLookUp("[DataColumn]", "[Table1]", "[ColumnA] = '"
& [Table1].[ColumnA] & "' AND [ColumnB] = '" & [Table1].[ColumnB] & "' AND " & _
"[DataColumn] IN (SELECT TOP " & (COUNT() / 2) _
& " [DataColumn] FROM [Table1] WHERE [ColumnA] = '"_
& [Table1].[ColumnA] & "' AND [ColumnB] = '"_
& [Table1].[ColumnB] & "' ORDER BY [DataColumn] ASC)) _
/ 2, DLookUp("[DataColumn]", "[Table1]", "[ColumnA] = '" _
& [Table1].[ColumnA] & "' AND [ColumnB] = '" & [Table1].[ColumnB] & "' AND " & _
"[DataColumn] IN (SELECT TOP " & ROUND(COUNT() / 2, 0, 1) _
& " [DataColumn] FROM [Table1] WHERE [ColumnA] = '"_
& [Table1].[ColumnA] & "' AND [ColumnB] = '"_
& [Table1].[ColumnB] & "' ORDER BY [DataColumn] ASC))) AS MedValue
FROM Table1
GROUP BY Table1.ColumnA, Table1.ColumnB;