473,396 Members | 1,884 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.

Median in a query

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 the median
of the another, though I'm not sure how. I've been able to add the
"Median" function (from the Microsoft Access Help Archive), but I can't
figure out how to incorporate that into the Totals. Do I need to use
the "expression" and then build the expression?

Thanks for any help,

Victor

Nov 13 '05 #1
8 14068
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Do you mean you are using the function provided in MS KB article
Q210581? If so you can include the call to the function in a query like
this:

SELECT Median(table_name, field_name), <other columns>
FROM table_name
WHERE <criteria>
GROUP BY Median(table_name, field_name) , <other columns>

Substitute your table & field name for "table_name" and "field_name,"
respectively.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQkHCjYechKqOuFEgEQLP0ACffvtQipID8zVlwFkn1btHfr/+7n8An0OZ
c4p03mDiHN7jo28XIOs3Y1VH
=seWN
-----END PGP SIGNATURE-----
ni*********@gmail.com wrote:
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 the median
of the another, though I'm not sure how. I've been able to add the
"Median" function (from the Microsoft Access Help Archive), but I can't
figure out how to incorporate that into the Totals. Do I need to use
the "expression" and then build the expression?

Nov 13 '05 #2
You're right, if you're looking in query design view, there is no median
function. I think there's everything else, sum, mean count etc.

I ran into this problem with my last job. We wound up having to run
Medians using an Access Ad In called "Total Access Statistics." This type
of thing is also really easy to do in something like SPSS.

You can do it in Excel, but it's drudgery arranging all the cells before
you can do the Medians. It may be possible to do with an SQL statement,
but I'm kind of new to writing SQL.

--
Message posted via http://www.accessmonster.com
Nov 13 '05 #3
Yes, that's the one.

Maybe I input the function wrong. When I try that I get an error
saying the function Median is not defined.

Nov 13 '05 #4
ni*********@gmail.com wrote:
Yes, that's the one.

Maybe I input the function wrong. When I try that I get an error
saying the function Median is not defined.


Make sure you've put the function in a regular module, not a form's
module. Make sure the function is a public function:

Public Function Median(....) As ....

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
Nov 13 '05 #5
I hate to treat you as a personal tutor, but I still can't seem to get
this to work.

SELECT Sample.Groups, Median(Sample, Numbers) As MedianOfNumbers
FROM Sample
GROUP BY Groups

The message now tells me that I Median() isn't part of an aggregate
function. Are aggregate functions only avg, count, min, max, etc?

If my table is named "Sample", and I have one field named "Groups"
(which i want to group by) and another named "Numbers" (from which I
want the median).
I tried just using

SELECT Median(Table, Field)
FROM Table;

I get a Parameter box that pops up. Does that mean that my table name
and field name are not recognized?

Thanks for all the help.

Nov 13 '05 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

When grouping you have to have an aggregate function around the Median()
function. Since the parameters are strings you have to delimit the
names of the table and column with quotes:

SELECT Groups, Sum(Median("Sample", "Numbers")) As MedianOfNumbers
FROM Sample
GROUP BY Groups

Use Sum or Avg or whichever aggregate function you want.

Since the Median() function uses DAO make sure you have the DAO library
checked in the References (VBA module, menu item Tools > References).
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQkNXHoechKqOuFEgEQKt1QCfTnXlYlqErSHgK3UxsSpU83 LOEHgAoOCj
t/QY1K1AnXd/vTwXU+fHLlkw
=nG6W
-----END PGP SIGNATURE-----

ni*********@gmail.com wrote:
I hate to treat you as a personal tutor, but I still can't seem to get
this to work.

SELECT Sample.Groups, Median(Sample, Numbers) As MedianOfNumbers
FROM Sample
GROUP BY Groups

The message now tells me that I Median() isn't part of an aggregate
function. Are aggregate functions only avg, count, min, max, etc?

If my table is named "Sample", and I have one field named "Groups"
(which i want to group by) and another named "Numbers" (from which I
want the median).
I tried just using

SELECT Median(Table, Field)
FROM Table;

I get a Parameter box that pops up. Does that mean that my table name
and field name are not recognized?

Thanks for all the help.

Nov 13 '05 #7
Thanks for all the help, I'm finally making some headway!

Nov 13 '05 #8
ni*********@gmail.com wrote:
Thanks for all the help, I'm finally making some headway!


Be sure to check out the method I posted earlier this month:

http://groups-beta.google.com/group/...2f7b4979f359ef

as an alternative to using the Median function. It's SQL only so it's
not as flexible as VBA, however it's more scalable :-). Note: When
pasting qryMedian from that post an extra '-' appears before the '<'
symbol that must be deleted.

Instead of:
SELECT Groups, Sum(Median("Sample", "Numbers")) As MedianOfNumbers FROM
Sample GROUP BY Groups

try this modification to MGFoster's SQL statement:
SELECT Groups, First(SELECT Median FROM qryMedian) As MedianOfNumbers
FROM Sample GROUP BY Groups

where:
qryRankForMedian:
SELECT Sample.Value, (SELECT Count(A.ID) FROM Sample AS A WHERE A.Value
< Sample.Value)+(SELECT Count(A.ID) FROM Sample AS A WHERE A.Value =
Sample.Value AND A.ID < Sample.ID)+1 AS Ranking, (SELECT Count(*) FROM
Sample)/2+0.5 AS WantRanking FROM Sample;

qryMedian:
SELECT Avg(qryRankForMedian.Value) AS Median FROM qryRankForMedian
WHERE (((Abs([Ranking]-[WantRanking]))<0.6));

Sample
ID Value Groups
1 1 A
2 2 A
3 3 A
4 4 B
5 5 B
6 5 B
7 5 C
8 6 C

The result looked like:
Groups MedianOfNumbers
A 4.5
B 4.5
C 4.5

If qryRankForMedian is changed to select only values within a group
(along with a suitable WHERE clause for WantRanking) it may be possible
to get the median of each group in its output line as well by using SQL
to reference the Groups value. To do that for the Median function
would require modification to accept the Groups value as an argument.
Of course the odds of someone needing this functionality are long. If
anyone needs that I'll try to flesh out the details.

That case would look like:
Groups MedianOfNumbers
A 2
B 5
C 5.5

James A. Fortune

Nov 13 '05 #9

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: Hugo L. | last post by:
I really don't know how to calculate the median. Can anybody help me?
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...
4
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
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...
1
by: Jaime Leivers | last post by:
Here's a median function in access that you can call in any query. You could change this to any excel function you wanted. Most people can find the windows help file that says how to call an excel...
0
by: Sebastien.LICHTHERTE | last post by:
Hello, I need to calculate the median and percentile of values in a group by query satisfying several criteria the user is asked to fill in when opening the query. A have a table called RX with...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.