Median Value

smiley1978

Board Regular
Joined
Sep 13, 2005
Messages
133
I have a query that I need to calculate a median value.

I need to group on three fields, STATE, DESCRIPTION, COVERAGE. I need to find the median value of the field DAYSOPEN.

I saw a thread about two or three weeks ago that covered this, but it was for a single field grouping.

Any help is appreciated.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I have a query that I need to calculate a median value.

I need to group on three fields, STATE, DESCRIPTION, COVERAGE. I need to find the median value of the field DAYSOPEN.

I saw a thread about two or three weeks ago that covered this, but it was for a single field grouping.

Any help is appreciated.

Here's a link to median and ms access. Hope it is helpful.

http://www.fabalou.com/Access/Modules/recordset_median.asp
 
Upvote 0
I have a query that is grouped by State, Description and Month. I have a function in VB that is designed to give me median by month only. See function below. I call it in the query. I need to adjust this to group by three columns in the query, not one. I do not understand what the function is actually doing so I cannot modify it myself.

Function.....................................

Option Compare Database
Option Explicit

Function Median(tName As String, fldName As String, Optional qField As String, Optional qValue As Long) 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()
If Len(qField) > 0 Then
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
"] FROM [" & tName & "] WHERE [" & qField & _
"] =" & qValue & " ORDER BY [" & fldName & "];")
Else
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
"] FROM [" & tName & "] WHERE [" & fldName & _
"] IS NOT NULL ORDER BY [" & fldName & "];")
End If
'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
ssMedian.Close
MedianDB.Close
End Function

SQL...........................................

SELECT Data.State, Data.Description, Data.Month, Median("Data","DaysOpen","Month",[Month]) AS MedianValue
FROM Data
GROUP BY Data.State, Data.Description, Data.Month
HAVING (((Data.Description)="Bodily Injury"));
 
Upvote 0
I have a table called Data. It has Seven fields: OpenDate(Date/Time), State(Text), Description(Text), PRO(Text), Month(Date/Time), DaysOpen(Number) and Test(Text).

I need to calculate the median value of DaysOpen based on the Grouping of State, Description and Month in a query. Someone provided me with a macro that I called from the query, but it was designed to calculate the median based on one field, not three.

I tried to concatenate the three fields into one field called Test, and then put that into the macro, but if gave me the not part aggrigate expression error.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top