Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Aggregate Median (UDF)
From Wiki
There is no Median in Jet SQL, unless it has been added for 2007, but here is an idea of how to get one. You will need ...
Some SQL ...
SELECT Statistics.Month, Sum(([SentTo])) AS [Sum Sent], fMedian("Statistics","Month",[Month],"SentTo") AS [Median Sent]
FROM Statistics
GROUP BY Statistics.Month;
And a User Defined Function (UDF).
- Function fMedian(SQLOrTable, GroupFieldName, GroupFieldValue, MedianFieldName)
- Dim rs As DAO.Recordset
- Set db = CurrentDb
- Set rs1 = db.OpenRecordset(SQLOrTable, dbOpenDynaset)
- If IsDate(GroupFieldValue) Then
- GroupFieldValue = "#" & GroupFieldValue & "#"
- ElseIf Not IsNumeric(GroupFieldValue) Then
- GroupFieldValue = "'" & Replace(GroupFieldValue, "'", "''") & "'"
- End If
- rs1.Filter = GroupFieldName & "=" & GroupFieldValue
- rs1.Sort = MedianFieldName
- Set rs = rs1.OpenRecordset()
- rs.Move (rs.RecordCount / 2)
- If rs.RecordCount Mod 2 = 0 Then
- varMedian1 = rs.Fields(MedianFieldName)
- rs.MoveNext
- fMedian = (varMedian1 + rs.Fields(MedianFieldName)) / 2
- Else
- fMedian = rs.Fields(MedianFieldName)
- End If
- End Function



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.