sluggercoach
Registered User.
- Local time
- Today, 10:32
- Joined
- Jun 12, 2007
- Messages
- 21
I have read through the forum and found many posts on how to calculate median. However, I am still having trouble. I am attempting to calculate median cycle times per station for an assembly. The result should look like this:
F0100 2.56
F0400 5.87
F0425 9.09
etc, etc.
Here is the code I am using. I am calling it from a query (SQL shown below). The result is that my stations are displayed but the median is for all of the values for all station displayed over and over. I cannot get it to show the median for each specific station. Any suggestions on what I am doing wrong?
Code
Function MedianT(pTable As String, pfield As String, Optional pgroup As String) As Single
'*******************************************
'Purpose: Return median value from a recordset
'Coded by: raskew
'Inputs: ? medianF("Orders", "Freight",[Value of Grouping field]) <enter>
'Output: 41.36 (may vary according to how much
' you've fiddled with this table).
'*******************************************
Dim rs As Recordset
Dim strSQL As String
Dim n As Integer
Dim sglHold As Single
If Len(pgroup) > 0 Then
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE type= '" & pgroup & "' and " & pfield & ">0 Order by " & pfield & ";"
Else
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE " & pfield & ">0 Order by " & pfield & ";"
End If
Set rs = CurrentDb.OpenRecordset(strSQL)
rs.MoveLast
n = rs.RecordCount
rs.Move -Int(n / 2)
If n Mod 2 = 1 Then 'odd number of elements
MedianT = rs(pfield)
Else 'even number of elements
sglHold = rs(pfield)
rs.MoveNext
sglHold = sglHold + rs(pfield)
MedianT = sglHold / 2
End If
rs.Close
End Function
Query SQL
SELECT mtest.Station, medianT("mtest","num") AS MEDIAN
FROM mtest
GROUP BY mtest.Station;
Result
MedianStationMEDIAN
3.41666674613953F01003.41666674613953F01303.41666674613953F01403.41666674613953F01603.41666674613953F01803.41666674613953F02103.41666674613953F02303.41666674613953F02503.41666674613953F02703.41666674613953F02903.41666674613953F03103.41666674613953F03303.41666674613953F03603.41666674613953F04103.41666674613953F04303.41666674613953F04503.41666674613953F04703.41666674613953F04903.41666674613953F05303.41666674613953F05603.41666674613953F05903.41666674613953F06103.41666674613953F06303.41666674613953F06503.41666674613953F07103.41666674613953F07203.41666674613953F07403.41666674613953F07903.41666674613953F08203.41666674613953F08603.41666674613953F08703.41666674613953F08903.41666674613953F09003.41666674613953F09203.41666674613953F09403.41666674613953F09703.41666674613953F10003.41666674613953F10203.41666674613953F10403.41666674613953F10603.41666674613953F11003.41666674613953F11303.41666674613953F11503.41666674613953
F0100 2.56
F0400 5.87
F0425 9.09
etc, etc.
Here is the code I am using. I am calling it from a query (SQL shown below). The result is that my stations are displayed but the median is for all of the values for all station displayed over and over. I cannot get it to show the median for each specific station. Any suggestions on what I am doing wrong?
Code
Function MedianT(pTable As String, pfield As String, Optional pgroup As String) As Single
'*******************************************
'Purpose: Return median value from a recordset
'Coded by: raskew
'Inputs: ? medianF("Orders", "Freight",[Value of Grouping field]) <enter>
'Output: 41.36 (may vary according to how much
' you've fiddled with this table).
'*******************************************
Dim rs As Recordset
Dim strSQL As String
Dim n As Integer
Dim sglHold As Single
If Len(pgroup) > 0 Then
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE type= '" & pgroup & "' and " & pfield & ">0 Order by " & pfield & ";"
Else
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE " & pfield & ">0 Order by " & pfield & ";"
End If
Set rs = CurrentDb.OpenRecordset(strSQL)
rs.MoveLast
n = rs.RecordCount
rs.Move -Int(n / 2)
If n Mod 2 = 1 Then 'odd number of elements
MedianT = rs(pfield)
Else 'even number of elements
sglHold = rs(pfield)
rs.MoveNext
sglHold = sglHold + rs(pfield)
MedianT = sglHold / 2
End If
rs.Close
End Function
Query SQL
SELECT mtest.Station, medianT("mtest","num") AS MEDIAN
FROM mtest
GROUP BY mtest.Station;
Result
MedianStationMEDIAN
3.41666674613953F01003.41666674613953F01303.41666674613953F01403.41666674613953F01603.41666674613953F01803.41666674613953F02103.41666674613953F02303.41666674613953F02503.41666674613953F02703.41666674613953F02903.41666674613953F03103.41666674613953F03303.41666674613953F03603.41666674613953F04103.41666674613953F04303.41666674613953F04503.41666674613953F04703.41666674613953F04903.41666674613953F05303.41666674613953F05603.41666674613953F05903.41666674613953F06103.41666674613953F06303.41666674613953F06503.41666674613953F07103.41666674613953F07203.41666674613953F07403.41666674613953F07903.41666674613953F08203.41666674613953F08603.41666674613953F08703.41666674613953F08903.41666674613953F09003.41666674613953F09203.41666674613953F09403.41666674613953F09703.41666674613953F10003.41666674613953F10203.41666674613953F10403.41666674613953F10603.41666674613953F11003.41666674613953F11303.41666674613953F11503.41666674613953