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.
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

jackd

Well-known Member
Joined
Oct 19, 2006
Messages
1,299
Office Version
  1. 365
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
 

smiley1978

Board Regular
Joined
Sep 13, 2005
Messages
133
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"));
 

smiley1978

Board Regular
Joined
Sep 13, 2005
Messages
133
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,564
Messages
5,596,875
Members
414,106
Latest member
Tigretto

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
Top