473,761 Members | 10,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14112
-----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_na me, field_name), <other columns>
FROM table_name
WHERE <criteria>
GROUP BY Median(table_na me, field_name) , <other columns>

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

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

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

iQA/AwUBQkHCjYechKq OuFEgEQLP0ACffv tQipID8zVlwFkn1 btHfr/+7n8An0OZ
c4p03mDiHN7jo28 XIOs3Y1VH
=seWN
-----END PGP SIGNATURE-----
ni*********@gma il.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*********@gma il.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:::mgf0 0 <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("Sam ple", "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:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

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

iQA/AwUBQkNXHoechKq OuFEgEQKt1QCfTn XlYlqErSHgK3Uxs SpU83LOEHgAoOCj
t/QY1K1AnXd/vTwXU+fHLlkw
=nG6W
-----END PGP SIGNATURE-----

ni*********@gma il.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*********@gma il.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("Sam ple", "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:
qryRankForMedia n:
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(qryRankForM edian.Value) AS Median FROM qryRankForMedia n
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 qryRankForMedia n 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
9715
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 http://mysql.progen.com.tr/doc/en/Group_by_functions.html. I am an experienced VB programmer that has recently moved to PHP/mySQL. My employer has a text file outputted from a vendor specific software with data. However it cannot be manipulated because it is text. I created a web that reads the...
2
9138
by: Hugo L. | last post by:
I really don't know how to calculate the median. Can anybody help me?
2
2526
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 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:
4
8180
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
9436
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 query it without making a table out of query 1. I can't find a median function in the "Total" field, so is there so way to make an expression to calculate the median of the orignial data from query 1 in my new query? Also, what does name by...
1
6503
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 function but don't know how to pass an array of the recordset they made into that function. This uses GetRows that nicely creates an array that can be passed into excel. I have this data table for testing. The table name is TestData ...
0
1699
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 this kind of data : HOSP TYPE SUB DAP A THO F 1
3
4672
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
4098
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 median of a recordset. The field must be a number value. Dim MedianTemp As Double Dim RstOrig As Recordset Set RstOrig = CurrentDb.OpenRecordset(RstName, dbOpenDynaset)
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.