473,396 Members | 1,804 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,396 software developers and data experts.

Finding Median - Gouped by field

Bob
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 Northwind no problem. I am
having some trouble though converting it to my specific purpose which
may be less complicated than the solution Bob (raskew) provided in the
thread.

Here are my specifics:

1) I only have one table (FactorTable) with two fields (neighborhood,
ratio).
2) I am looking for the Median of the 'ratio' field for EACH
'neighborhood'.

For Example:
Hood Ratio
501 1
501 2
501 3
601 2
601 4
601 6
etc....

I need a resulting table to spit out:

Hood Median
501 2
601 4

....or at least:

Hood Median
501 2
501 2
501 2
601 4
601 4
601 4
etc....

I have seen this questions posted in several forums (and groups) but
have yet to see an answer.
Can this be done????

Thanks alot in advance!

Bob

Nov 13 '05 #1
2 2506
Bob, the following function will do what you are requesting. Before
running it you will need to create a query called MedianQuery, whose
SQL is:
SELECT FactorTable.neighbourhood, FactorTable.ratio
FROM FactorTable
WHERE ((Not (FactorTable.neighbourhood) Is Null) AND (Not
(FactorTable.ratio) Is Null))
ORDER BY FactorTable.neighbourhood, FactorTable.ratio;

Public Function CreateMedianTable()

Dim dbs As DAO.Database, tdf As DAO.TableDef, fld As DAO.Field,
fld2 As DAO.Field
On Error GoTo Err_median
Set dbs = CurrentDb
dbs.TableDefs.Delete "Median Table"
dbs.TableDefs.Refresh

Set tdf = dbs.CreateTableDef("Median Table")
Set fld = tdf.CreateField("Category", dbText, 40)
Set fld2 = tdf.CreateField("Median value", dbSingle, 40)
tdf.Fields.Append fld
tdf.Fields.Append fld2
tdf.Fields.Refresh
dbs.TableDefs.Append tdf
dbs.TableDefs.Refresh

Dim rst As DAO.Recordset
Dim rstM As DAO.Recordset
Dim myarray(2000) As Variant
Dim title1 As String
Dim fieldname As String
Dim medianresult As Variant
Dim A As Integer
Dim B As Integer
title1 = "MedianQuery"
Dim appXL As New Excel.Application

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(title1)
Set rstM = dbs.OpenRecordset("Median Table")
rst.MoveFirst
Do While Not rst.EOF
fieldname = rst.Fields(0).Value
B = 1
Do While rst.Fields(0) = fieldname
myarray(B) = rst.Fields(1)
rst.MoveNext
If rst.EOF Then Exit Do
B = B + 1
Loop
medianresult = appXL.Median(myarray)
rstM.AddNew
rstM.Fields("Category") = fieldname
rstM.Fields("Median value") = medianresult
rstM.Update
Erase myarray 'sets each element to empty
Loop
rst.Close
rstM.Close
dbs.Close
appXL.Quit

DoCmd.OpenTable ("Median Table")

Exit_median:
Exit Function

Err_median:
If Err.Number = 3265 Then
Resume Next
Else
MsgBox Error$
Resume Exit_median
End If

End Function

Nov 13 '05 #2
I forgot to add that you will need Microsoft Excel on your computer as
well as a reference to Microsoft Excel in your access database.

Mike

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

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...
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...
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
3
by: Storm9 | last post by:
I have to: "Use functional decomposition to write a C++ program that determines the median of five input numbers. The median is the middle number when the five are arranged in order. However, the...
7
by: Bhadan | last post by:
Hello, I have several jagged arrays which have been sorted. I'm trying to find the median of each array. Any tips appreciated. TIA. Bhads.
3
by: mehwishobaid | last post by:
i dont know wat is wrong with my code. when i compile. i get the error saying line 29: error: expression must have pointer-to-object type #include <iostream> using namespace std; #include...
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: 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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.