473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Median of Different Field Groups

Hello,

I have a table of variables (X1, X2, X3...) that each have multiple measurements (Y1.1, Y1.2, Y1.3, Y2.1...)

Variable Measurement
X1 Y1.1
X1 Y1.2
X1 Y1.3
X1 Y1.4
X2 Y2.1
X2 Y2.2
X2 Y2.3
X3 Y3.1
X3 Y3.2
X3 Y3.3

I ned to find the median Y within each X group. I've used the "avg" from the total drop down before but it doesn't look like there is a selection for median.

Is there a way to find these medians?
Thanks
Aug 26 '08 #1
9 1547
Stewart Ross
2,545 Expert Mod 2GB
Hi. Simplest way is not automatic. Group the data using a Group By (Totals) query on X and Y, and sort in order on both fields. The median is the middle value of each ordered set (or the average of the two mid values if there are an even number of values in the set).

If you need to use a function to find the median this MS knowlede base article provides the code for doing so. You would need to feed it with a query which restricts the grouping to one set of Y data at a time (perhaps using a subquery to do this), as the function is not set up to find multiple medians simultaneously.

-Stewart
Aug 26 '08 #2
OK, thanks for your help, I've got the module running, but it is returning incorrect values. In fact, the values it is returning as medians, are not even values included in the table. See any mistakes?
-Nate

Public Function fCalculateMedian(strfleetname As String, dblmotorMWHRs As Double)
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MySQL As String
Dim intNumOfRecords As Integer, curMotorMWHR As Double

MySQL = "SELECT tbl_MWHRSday.FLEET_NAME, tbl_MWHRSday.avg_mwhrs FROM tbl_MWHRSday "
MySQL = MySQL & "WHERE tbl_MWHRSday.Fleet_Name='" & strfleetname & "' ORDER BY tbl_MWHRSday.Fleet_Name, tbl_MWHRSday.avg_mwhrs;"

Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset(MySQL, dbOpenSnapshot)

MyRS.MoveLast: MyRS.MoveFirst

intNumOfRecords = MyRS.RecordCount
If intNumOfRecords = 0 Then Exit Function

If intNumOfRecords Mod 2 = 0 Then 'Even number of Records
MyRS.Move (intNumOfRecords \ 2) - 1 'Move half-way point
dblmotorMWHRs = MyRS![avg_mwhrs] '1st value to average
MyRS.MoveNext
curMotorMWHR = curMotorMWHR + MyRS![avg_mwhrs] '2nd value to average added to 1st value
fCalculateMedian = curMotorMWHR / 2 'Average them out
Else 'Odd number of Records
MyRS.Move (intNumOfRecords \ 2)
fCalculateMedian = MyRS![avg_mwhrs]
End If

MyRS.Close
End Function
Aug 27 '08 #4
FishVal
2,653 Expert 2GB
Looks more or less ok, but what for do you pass dblmotorMWHRs argument to the function?

Kind regards,
Fish

P.S. Though not sure, I think you may encounter a problem using the function in query. Query may lock your table to open it with DAO. If it happens, then try to use ADO instead.
Aug 27 '08 #5
Looks more or less ok, but what for do you pass dblmotorMWHRs argument to the function?

Kind regards,
Fish

P.S. Though not sure, I think you may encounter a problem using the function in query. Query may lock your table to open it with DAO. If it happens, then try to use ADO instead.
I'm not sure wha the purpose is, I based the code from http://bytes.com/forum/thread655107.html

If I don't include it I get and error "Argument count mismatch"
Aug 27 '08 #6
ADezii
8,834 Expert 8TB
OK, thanks for your help, I've got the module running, but it is returning incorrect values. In fact, the values it is returning as medians, are not even values included in the table. See any mistakes?
-Nate

Public Function fCalculateMedian(strfleetname As String, dblmotorMWHRs As Double)
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MySQL As String
Dim intNumOfRecords As Integer, curMotorMWHR As Double

MySQL = "SELECT tbl_MWHRSday.FLEET_NAME, tbl_MWHRSday.avg_mwhrs FROM tbl_MWHRSday "
MySQL = MySQL & "WHERE tbl_MWHRSday.Fleet_Name='" & strfleetname & "' ORDER BY tbl_MWHRSday.Fleet_Name, tbl_MWHRSday.avg_mwhrs;"

Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset(MySQL, dbOpenSnapshot)

MyRS.MoveLast: MyRS.MoveFirst

intNumOfRecords = MyRS.RecordCount
If intNumOfRecords = 0 Then Exit Function

If intNumOfRecords Mod 2 = 0 Then 'Even number of Records
MyRS.Move (intNumOfRecords \ 2) - 1 'Move half-way point
dblmotorMWHRs = MyRS![avg_mwhrs] '1st value to average
MyRS.MoveNext
curMotorMWHR = curMotorMWHR + MyRS![avg_mwhrs] '2nd value to average added to 1st value
fCalculateMedian = curMotorMWHR / 2 'Average them out
Else 'Odd number of Records
MyRS.Move (intNumOfRecords \ 2)
fCalculateMedian = MyRS![avg_mwhrs]
End If

MyRS.Close
End Function
Try:
Expand|Select|Wrap|Line Numbers
  1. If intNumOfRecords Mod 2 = 0 Then 'Even number of Records
  2.   MyRS.Move (intNumOfRecords \ 2) - 1 'Move half-way point
  3.   dblmotorMWHRs = MyRS![avg_mwhrs] '1st value to average
  4.     MyRS.MoveNext
  5.   dblmotorMWHRs = dblmotorMWHRs + MyRS![avg_mwhrs] '2nd value to average added to 1st value
  6.     fCalculateMedian = dblmotorMWHRs / 2 'Average them out
  7. Else 'Odd number of Records
  8.   MyRS.Move (intNumOfRecords \ 2)
  9.   fCalculateMedian = MyRS![avg_mwhrs]
  10. End If
Aug 27 '08 #7
FishVal
2,653 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. MyRS.Move (intNumOfRecords \ 2) - 1   'Move half-way point
  2.  
should be
Expand|Select|Wrap|Line Numbers
  1. MyRS.Move (intNumOfRecords \ 2 - 1)   'Move half-way point
  2.  
Aug 27 '08 #8
Try:
Expand|Select|Wrap|Line Numbers
  1. If intNumOfRecords Mod 2 = 0 Then 'Even number of Records
  2.   MyRS.Move (intNumOfRecords \ 2) - 1 'Move half-way point
  3.   dblmotorMWHRs = MyRS![avg_mwhrs] '1st value to average
  4.     MyRS.MoveNext
  5.   dblmotorMWHRs = dblmotorMWHRs + MyRS![avg_mwhrs] '2nd value to average added to 1st value
  6.     fCalculateMedian = dblmotorMWHRs / 2 'Average them out
  7. Else 'Odd number of Records
  8.   MyRS.Move (intNumOfRecords \ 2)
  9.   fCalculateMedian = MyRS![avg_mwhrs]
  10. End If
Yup, that's it, just caught it myself.
Thanks for your help!
Aug 27 '08 #9
ADezii
8,834 Expert 8TB
Yup, that's it, just caught it myself.
Thanks for your help!
You are quite welcome, denveromlp.
Aug 27 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ross Contino | last post by:
Hello to all: I have been searching the web for examples on how to determine a median value in a mySQL table. I have reviewed the article at...
2
by: michael way | last post by:
I read the follow query about calculating median posted by Daivd Porta on 10/8/03. CREATE TABLE SomeValues (keyx CHAR(1) PRIMARY KEY, valuex INTEGER NOT NULL) INSERT INTO SomeValues VALUES...
2
by: Bob | last post by:
I have been looking at the code for MedianFind(pDte As String) from the following thread from UtterAccess.com: "Finding Median average grouped by field" I have been able to get it to run using...
8
by: nick.vitone | last post by:
Hi, I'm somewhat of a novice at Access, and I have no experience programming whatsoever. I'm attempting to calculate the statistical median in a query. I need to "Group by" one column and find...
0
by: jimfortune | last post by:
In http://groups-beta.google.com/group/comp.databases.ms-access/msg/46197725b88fc3fd?hl=en I said: If qryRankForMedian is changed to select only values within a group (along with a suitable...
3
by: gunnarnime | last post by:
Can I get the following function to work with Fieldnames that have spaces in it? I've tried spqaure brackets to no avail. Any help would be appreciated. Public Function DMedian(FieldName As...
5
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
3
by: Scott | last post by:
I need to take the median from a field of records in a report. Can someone shed the light how to do it. Thanks, Scott
6
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.