473,411 Members | 2,272 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,411 software developers and data experts.

calculating a median within a query

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 string mean?

Thanks.

Jul 7 '06 #1
5 9394
jonm4102 wrote:
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 string mean?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I've never seen the expression "name by string."

Here is a median finding query that I found on the web. It is set up
for T-SQL, but can be used in Access SQL; just change the names of the
tables/columns.

- -- From: http://www.aspfaq.com/show.asp?id=2506

- -- Find the median value

- -- For odd Count(*)

SELECT TOP 1 splunge FROM
(
SELECT TOP 50 PERCENT splunge
FROM blat ORDER BY splunge
) sub
ORDER BY splunge DESC

- -- For even Count(*). Can be used for odd Count(*) also.

SELECT AVG(splunge) FROM
(
SELECT splunge FROM (
SELECT TOP 1 splunge = splunge * 1.0 FROM
(
SELECT TOP 50 PERCENT splunge
FROM blat ORDER BY splunge
) sub_a
ORDER BY 1 DESC
) sub_1
UNION ALL
SELECT splunge FROM (
SELECT TOP 1 splunge = splunge * 1.0 FROM
(
SELECT TOP 50 PERCENT splunge
FROM blat ORDER BY splunge DESC
) sub_b
ORDER BY 1
) sub_2
) median
--
MGFoster:::mgf00 <atearthlink <decimal-pointnet
Oakland, CA (USA)
** Respond only to this newsgroup. I DO NOT respond to emails **

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

iQA/AwUBRK69c4echKqOuFEgEQK17QCgqqpRlyjWPsDoyDV7KwOO5K I/oUAAoJLV
BH3e8tVeYy7JbacvZJMQfDG7
=cNR0
-----END PGP SIGNATURE-----
Jul 7 '06 #2

jonm4102 wrote:
I'm trying to calculate the median of some numerical data ....
My almost totally untested effort:

Dim s(2) As String

s(0) = "SELECT TOP 1 sq1.Score AS Median FROM"
s(0) = s(0) & vbNewLine
s(0) = s(0) & "[SELECT TOP 50 PERCENT Score FROM Query3 ORDER BY Score
ASC]. sq1"
s(0) = s(0) & vbNewLine
s(0) = s(0) & "ORDER BY sq1.Score DESC"

s(1) = Replace(s(0), "ORDER BY Score ASC", "ORDER BY Score DESC")
s(1) = Replace(s(1), "ORDER BY sq1.Score DESC", "ORDER BY sq1.Score
ASC")

With CurrentProject.Connection
Debug.Print _
(.Execute(s(0)).Collect("Median") + _
.Execute(s(1)).Collect("Median")) / 2
End With

Jul 7 '06 #3
jonm4102 wrote:
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 string mean?

Thanks.
I use SQL for finding medians. See:

http://groups.google.com/group/comp....b6eb508f22fa9d

Click back a link or two to find SQL for the median of non-grouped
data.

I hope this helps,

James A. Fortune
CD********@FortuneJames.com

Jul 7 '06 #4
Thanks for your previous post. I've run into another hurdle, and would
greatly appreciate any help you may be able to offer. I have revenue
data sorted by month and years for a number of distinctly labeled
stores (imagine a Center ID). I would like to find the percentage of
"gainers" (stores whose monthly revenues exceed the previous month's
revenues) out of the all the stores. I know how to determine this by
using an individual queries to sort out each store's revenue by year
and month, and then use another query to classify gainers v. decliners,
and then (Fnally!) calculate the percentages. Is there an easier way to
do this than to run around 50 queries (one for each month for 4 years)?
Thanks.

Jonathan


CD********@FortuneJames.com wrote:
jonm4102 wrote:
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 string mean?

Thanks.

I use SQL for finding medians. See:

http://groups.google.com/group/comp....b6eb508f22fa9d

Click back a link or two to find SQL for the median of non-grouped
data.

I hope this helps,

James A. Fortune
CD********@FortuneJames.com
Jul 17 '06 #5
jonm4102 wrote:
Thanks for your previous post. I've run into another hurdle, and would
greatly appreciate any help you may be able to offer. I have revenue
data sorted by month and years for a number of distinctly labeled
stores (imagine a Center ID). I would like to find the percentage of
"gainers" (stores whose monthly revenues exceed the previous month's
revenues) out of the all the stores. I know how to determine this by
using an individual queries to sort out each store's revenue by year
and month, and then use another query to classify gainers v. decliners,
and then (Fnally!) calculate the percentages. Is there an easier way to
do this than to run around 50 queries (one for each month for 4 years)?
Thanks.

Jonathan


CD********@FortuneJames.com wrote:
jonm4102 wrote:
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 string mean?
>
Thanks.
I use SQL for finding medians. See:

http://groups.google.com/group/comp....b6eb508f22fa9d

Click back a link or two to find SQL for the median of non-grouped
data.

I hope this helps,

James A. Fortune
CD********@FortuneJames.com
Jonathan,

Your problem seems to be getting away from median calculations. Try
the following thread:

http://groups.google.com/group/comp....bdfadf979aa742

Maybe you can curve-fit the slope for a period of several years (the 48
monthly medians for each Center ID?) all at once and use the sign of
the slope to determine gainers and losers. You're still going to have
to do a lot of work and you also have to do the percentage calculation
so any shortcuts have to include those steps. It's all possible in
SQL, but also consider doing everything in VBA because of the amount of
sequential calculation required. The percentage part is not difficult
in SQL because you can sum IIf([Gainer], 1, 0) divided by the number of
query records (another sum) to get the fraction of gainers. So the SQL
approach would need a query to get the medians, a query to get the
slopes and a query to get the percentages. It's possible that a
crosstab query can handle all this at once but for such a complex query
I'd want to see the results of the three individuals to cross-check the
results (no pun intended). Even a second totals query on the crosstab
wouldn't be too bad. The Sgn() function can be used on the slope
calculation to return a -1, 0 or 1 for the Value (Gainer) field in the
(wide) crosstab. The more I think about it, the better three separate
queries look.

James A. Fortune
CD********@FortuneJames.com

The bedraggled young woman showed up at the steps of a convent. The
convent took the woman in and had a priest talk to her. "How did you
come to such a sad state of affairs?" he asked. "I started out snow
white," she replied, "but then drifted."

Jul 18 '06 #6

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?
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...
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
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...
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: Emlyons | last post by:
Hello, I am completely new to Access. I am running a query but need to take the median value of observations in one of my tables. I've tried creating an expression but it is not working. I've also...
10
by: Massimo30 | last post by:
Looking for answers and have done a search in books and websites and still can't find the answer: I am setting up a Market Trend DB on housing figures. I have the following fields in my table: My...
1
by: cmb3587 | last post by:
My code runs fine for the most part...the only time it fails is when I type in a negative to end the array. I don't want the negative number to be included in the array and I thought that is what...
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
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:
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.