Calculate MEDIAN in queries

D

Dirk

I want to calculate the Median in a Microsoft Access
query. How do I do this?

Our office is using Excel to process large amounts of
water quality data. PivotTables have helped summarize
some of the data. Unfortunately, there is no Median
function available in Excel's PivotTables (why?).

We brought the data into an Access 2002 table, and then
created a simple query grouping the water data by river
segment and then season. Unfortunately, there is no
function to allow us to calculate the median in Access
queries.


Sincerely,
Dirk

North/South Consultants Inc.
Winnipeg, Manitoba
Canada

(Please post your reply to this newsgroup - thanks :))
 
D

Dirk

Dear Mr. Vanderghast.

Thank you for pointing me to the code. I created a Module
in Access and pasted the code. I then entered the
function in the Access query. Where it says "x" for
percentile, I entered 0.5. However, I am seeing aggregate
errors or a message box asking me to enter a Table name -
even though I specified the table name in the function in
the query.

This sounds crazy, but how exactly should I use the
function?

Sincerely,
Dirk
 
L

LeAnne

Hi Dirk,

Water quality monitoring, eh? Greetings from a fellow aquatic ecologist!

Unfortunately, there's no easy way. Unlike AVG(), STDEV(), and other
measures of central tendency, Access has no "built-in" function for
calculating the median. One possibility is to create your own custom
Function using VBA. You'll find some example code at

http://support.microsoft.com/default.aspx?scid=kb;en-us;210581&Product=acc

Another option would be to call the Excel MEDIAN() worksheet function
from within Access. Read about it at

http://support.microsoft.com/default.aspx?scid=kb;en-us;153748&Product=acc

or look in the OLH under Automation.

A third option would be to download the test version of FMS Inc.'s Total
Access Statistics, which can easily calculate mean, median, mode,
standard deviation, standard error, MSE, and all those other descriptive
statistics (among other goodies). Note that the trial version only
works on tables, not queries, and it expires after a set amount of time.
Read about it at http://www.fmsinc.com/products/statistics/index.html
It's an awesome tool, I use it all the time.

hth,

LeAnne
 
M

Michel Walsh

Hi,


You have a NULL value in the field? Otherwise, you could use the
function, in the immediate debug window, as example, like:


? XPercentile( "fieldName", "TableName", 0.5 )



where field name and table name are the real one, and between double quotes
(as string).



Hoping it may help,
Vanderghast, Access MVP
 
M

Michel Walsh

Hi,



It also assumes that there is no space in the table name, and that the
table name is legal, without having to use [ ].



Vanderghast, Access MVP
 
D

Dirk

Hi LeAnne.

Thank you kindly for your reply. I will try these out. :)

I suspect Microsoft thought users would use other
software to calc the Median, rather than use Excel
PivotTables or Access queries. They don't realize how
much Excel and Access is used in the sciences. We spend a
lot of time moving blocks of data around in Excel. I'm
sure you know what I mean.

Sincerely,
Dirk
 
D

Dirk

Hello,

Thanks again for your reply. I don't have any NULL
values, but I did have a space in one of the field names
used. I will try this again.

I also wonder whether I should be using a second query
based on the initial one, instead of relying on one
single query to compute the Medians. What do you think?

I am using the View, Totals command in the query to group
data: first by river segment, then by season, then by
parameter, then by form, and then comes the actual
numerical result. For example, for Athabasca River would
have four seasons of data (fall, winter, etc.). Then for
a parameter like Aluminum Dissolved, I would have several
results - for which I need to calc the median (because it
is less sensitive to outliers - a common practice in
water quality analysis).

Is it also possible to call up Excel's MEDIAN function
from within Access 2002?

Sincerely,
Dirk

North/South Consultants Inc.
Winnipeg, Manitoba
Canada
www.nscons.ca

-----Original Message-----
Hi,



It also assumes that there is no space in the table name, and that the
table name is legal, without having to use [ ].



Vanderghast, Access MVP


Dear Mr. Vanderghast.

Thank you for pointing me to the code. I created a Module
in Access and pasted the code. I then entered the
function in the Access query. Where it says "x" for
percentile, I entered 0.5. However, I am seeing aggregate
errors or a message box asking me to enter a Table name -
even though I specified the table name in the function in
the query.

This sounds crazy, but how exactly should I use the
function?

Sincerely,
Dirk


.
 
M

Michel Walsh

Hi,


You can, technically, but through a VBA function: a VBA function would
call your Excel-VBA method, and then assuming you have the "Reference..."
rightly set, and the arguments of your Excel method properly "typed", ...
Maybe it is preferable to rank "by category", and find the 50 centile.


Assuming a table like


River quarter Test Measure ' fields
Athabasca winter2001 Al 0.0001 ' data sample

then a first query like:

=============
SELECT a.river, a.quarter, a.test, a.measure, COUNT(*) as rank

FROM myData As a INNER JOIN myData As b
ON a.river = b.river
AND a.test= b.test
AND a.quarter = b.quarter
AND a.measure >= b.measure

GROUP BY a.river, a.quarter, a.test, a.measure

HAVING COUNT(*) >= 0.5* (SELECT COUNT(*)
FROM myTable As c
WHERE a.river=c.river
AND a.test= c.test
AND a.quarter=b.quarter)

===============


would return the upper half most test measure. In fact, it counts the number
of test-value, measure, lower or equal to itself, for the same river, same
quarter, same test. Save that query, say, q1, then


SELECT river, quarter, test, MIN(measure) as Centile50
FROM q1
GROUP BY river, quarter, test



would supply the minimum value of that upper half, or the 50 centile, per
river, per quarter, per test.


If you don't have a quarter field, but a dateTimeStamp, instead of
z.quarter, use Format( z.DateTimeStamp, "q-yyyy"), as example (but can be
quite slow, since not indexed).




Hoping it may help,
Vanderghast, Access MVP

Dirk said:
Hello,

Thanks again for your reply. I don't have any NULL
values, but I did have a space in one of the field names
used. I will try this again.

I also wonder whether I should be using a second query
based on the initial one, instead of relying on one
single query to compute the Medians. What do you think?

I am using the View, Totals command in the query to group
data: first by river segment, then by season, then by
parameter, then by form, and then comes the actual
numerical result. For example, for Athabasca River would
have four seasons of data (fall, winter, etc.). Then for
a parameter like Aluminum Dissolved, I would have several
results - for which I need to calc the median (because it
is less sensitive to outliers - a common practice in
water quality analysis).

Is it also possible to call up Excel's MEDIAN function
from within Access 2002?

Sincerely,
Dirk

North/South Consultants Inc.
Winnipeg, Manitoba
Canada
www.nscons.ca

-----Original Message-----
Hi,



It also assumes that there is no space in the table name, and that the
table name is legal, without having to use [ ].



Vanderghast, Access MVP


Dear Mr. Vanderghast.

Thank you for pointing me to the code. I created a Module
in Access and pasted the code. I then entered the
function in the Access query. Where it says "x" for
percentile, I entered 0.5. However, I am seeing aggregate
errors or a message box asking me to enter a Table name -
even though I specified the table name in the function in
the query.

This sounds crazy, but how exactly should I use the
function?

Sincerely,
Dirk



-----Original Message-----
Hi,


If the 50 percentile is OK for you, as approximation,
then
http://www.mvps.org/access/queries/qry0019.htm can be
interesting (note that
the article assumes there is no NULL value in the field).



Hoping it may help,
Vanderghast, Access MVP



I want to calculate the Median in a Microsoft Access
query. How do I do this?

Our office is using Excel to process large amounts of
water quality data. PivotTables have helped summarize
some of the data. Unfortunately, there is no Median
function available in Excel's PivotTables (why?).

We brought the data into an Access 2002 table, and then
created a simple query grouping the water data by river
segment and then season. Unfortunately, there is no
function to allow us to calculate the median in Access
queries.


Sincerely,
Dirk

North/South Consultants Inc.
Winnipeg, Manitoba
Canada

(Please post your reply to this newsgroup - thanks :))




.


.
 
G

Guest

Hi,
I've tried using the query below (adapted to my data table) and it takes
about 6 minutes to run. I have about 27000 records and I would like to find
out the median on several different fields. I also don't think that it's
giving me the right answer. I have a good idea where it should lie and it's
far too low. I can easily find out the record number if they are ordered,
but I can't reference it. Is there an easy command to select the 500th
record?

Thankyou

Ian Belcher

Michel Walsh said:
Hi,


You can, technically, but through a VBA function: a VBA function would
call your Excel-VBA method, and then assuming you have the "Reference..."
rightly set, and the arguments of your Excel method properly "typed", ...
Maybe it is preferable to rank "by category", and find the 50 centile.


Assuming a table like


River quarter Test Measure ' fields
Athabasca winter2001 Al 0.0001 ' data sample

then a first query like:

=============
SELECT a.river, a.quarter, a.test, a.measure, COUNT(*) as rank

FROM myData As a INNER JOIN myData As b
ON a.river = b.river
AND a.test= b.test
AND a.quarter = b.quarter
AND a.measure >= b.measure

GROUP BY a.river, a.quarter, a.test, a.measure

HAVING COUNT(*) >= 0.5* (SELECT COUNT(*)
FROM myTable As c
WHERE a.river=c.river
AND a.test= c.test
AND a.quarter=b.quarter)

===============


would return the upper half most test measure. In fact, it counts the number
of test-value, measure, lower or equal to itself, for the same river, same
quarter, same test. Save that query, say, q1, then


SELECT river, quarter, test, MIN(measure) as Centile50
FROM q1
GROUP BY river, quarter, test



would supply the minimum value of that upper half, or the 50 centile, per
river, per quarter, per test.


If you don't have a quarter field, but a dateTimeStamp, instead of
z.quarter, use Format( z.DateTimeStamp, "q-yyyy"), as example (but can be
quite slow, since not indexed).




Hoping it may help,
Vanderghast, Access MVP

Dirk said:
Hello,

Thanks again for your reply. I don't have any NULL
values, but I did have a space in one of the field names
used. I will try this again.

I also wonder whether I should be using a second query
based on the initial one, instead of relying on one
single query to compute the Medians. What do you think?

I am using the View, Totals command in the query to group
data: first by river segment, then by season, then by
parameter, then by form, and then comes the actual
numerical result. For example, for Athabasca River would
have four seasons of data (fall, winter, etc.). Then for
a parameter like Aluminum Dissolved, I would have several
results - for which I need to calc the median (because it
is less sensitive to outliers - a common practice in
water quality analysis).

Is it also possible to call up Excel's MEDIAN function
from within Access 2002?

Sincerely,
Dirk

North/South Consultants Inc.
Winnipeg, Manitoba
Canada
www.nscons.ca

-----Original Message-----
Hi,



It also assumes that there is no space in the table name, and that the
table name is legal, without having to use [ ].



Vanderghast, Access MVP


Dear Mr. Vanderghast.

Thank you for pointing me to the code. I created a Module
in Access and pasted the code. I then entered the
function in the Access query. Where it says "x" for
percentile, I entered 0.5. However, I am seeing aggregate
errors or a message box asking me to enter a Table name -
even though I specified the table name in the function in
the query.

This sounds crazy, but how exactly should I use the
function?

Sincerely,
Dirk



-----Original Message-----
Hi,


If the 50 percentile is OK for you, as approximation,
then
http://www.mvps.org/access/queries/qry0019.htm can be
interesting (note that
the article assumes there is no NULL value in the field).



Hoping it may help,
Vanderghast, Access MVP



I want to calculate the Median in a Microsoft Access
query. How do I do this?

Our office is using Excel to process large amounts of
water quality data. PivotTables have helped summarize
some of the data. Unfortunately, there is no Median
function available in Excel's PivotTables (why?).

We brought the data into an Access 2002 table, and then
created a simple query grouping the water data by river
segment and then season. Unfortunately, there is no
function to allow us to calculate the median in Access
queries.


Sincerely,
Dirk

North/South Consultants Inc.
Winnipeg, Manitoba
Canada

(Please post your reply to this newsgroup - thanks :))




.



.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top