M
Matt
Hi all,
I have a group by query with two fields p_code and cost_code that I need to
calculate the median on the weight. I'm running into trouble because the
table is about 100k records with 133 unique p_code and cost_code combinations.
With my current median function, it only calculates the weight of the whole
list and not by my group by combination (pcode and cost_code).
Is there a way to calculate the median using my group by set of data? If
not, I have to create 133 separate tables and calculate the median in each
table.
Here's the code I was using to calculate the median (taken from microsoft
knowledge base and only works for one set of data):
Option Compare Database
Option Explicit
Function Median(tName As String, fldName As String) As Single
Dim MedianDB As DAO.Database
Dim ssMedian As DAO.Recordset
Dim RCount As Integer, i As Integer, x As Double, y As Double, _
OffSet As Integer
Set MedianDB = CurrentDb()
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
"] FROM [" & tName & "] WHERE [" & fldName & _
"] IS NOT NULL ORDER BY [" & fldName & "];")
'NOTE: To include nulls when calculating the median value, omit
'WHERE [" & fldName & "] IS NOT NULL from the example.
ssMedian.MoveLast
RCount% = ssMedian.RecordCount
x = RCount Mod 2
If x <> 0 Then
OffSet = ((RCount + 1) / 2) - 2
For i% = 0 To OffSet
ssMedian.MovePrevious
Next i
Median = ssMedian(fldName)
Else
OffSet = (RCount / 2) - 2
For i = 0 To OffSet
ssMedian.MovePrevious
Next i
x = ssMedian(fldName)
ssMedian.MovePrevious
y = ssMedian(fldName)
Median = (x + y) / 2
End If
If Not ssMedian Is Nothing Then
ssMedian.Close
Set ssMedian = Nothing
End If
Set MedianDB = Nothing
End Function
Thanks in advance for your help.
Best Regards,
Matt
I have a group by query with two fields p_code and cost_code that I need to
calculate the median on the weight. I'm running into trouble because the
table is about 100k records with 133 unique p_code and cost_code combinations.
With my current median function, it only calculates the weight of the whole
list and not by my group by combination (pcode and cost_code).
Is there a way to calculate the median using my group by set of data? If
not, I have to create 133 separate tables and calculate the median in each
table.
Here's the code I was using to calculate the median (taken from microsoft
knowledge base and only works for one set of data):
Option Compare Database
Option Explicit
Function Median(tName As String, fldName As String) As Single
Dim MedianDB As DAO.Database
Dim ssMedian As DAO.Recordset
Dim RCount As Integer, i As Integer, x As Double, y As Double, _
OffSet As Integer
Set MedianDB = CurrentDb()
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
"] FROM [" & tName & "] WHERE [" & fldName & _
"] IS NOT NULL ORDER BY [" & fldName & "];")
'NOTE: To include nulls when calculating the median value, omit
'WHERE [" & fldName & "] IS NOT NULL from the example.
ssMedian.MoveLast
RCount% = ssMedian.RecordCount
x = RCount Mod 2
If x <> 0 Then
OffSet = ((RCount + 1) / 2) - 2
For i% = 0 To OffSet
ssMedian.MovePrevious
Next i
Median = ssMedian(fldName)
Else
OffSet = (RCount / 2) - 2
For i = 0 To OffSet
ssMedian.MovePrevious
Next i
x = ssMedian(fldName)
ssMedian.MovePrevious
y = ssMedian(fldName)
Median = (x + y) / 2
End If
If Not ssMedian Is Nothing Then
ssMedian.Close
Set ssMedian = Nothing
End If
Set MedianDB = Nothing
End Function
Thanks in advance for your help.
Best Regards,
Matt