Hello!
I have a table wich contains product number and different time date attachet pr product number.
Today we have used to calculate MEAN to get an average time consumed pr product. We have now learned that MEDIAN would give us more quality data since we sometimes have abnormalities on time consumed.
However. Access 2007 does not seem to have a MEDIAN function, and when i google it i only find code to do this for an entire dataset.
I have 10000 different products in the table, all with 10-50 time consumed rows. I also need to update our MEDIAN time consumed data pr product a couple of times a day. If i should use the code that M$ provides i have to close and open a connection to the table 10000 to get the results that i want
http://support.microsoft.com/kb/q95918/
Anybody have seen another solution for this?
Here is a little Function that I created that is used within the context of a Calculated Field in a DISTINCT Query. In this particular case, it accepts two Arguments: Distinct Field and Field to calculate the actual Median Value. In this specific instance, I calculated the Median Value for properties by Area. I'll post the SQL, Function Definition, Sample Data, and Query Execution results for clarification. - SQL Statement
- SELECT DISTINCT tblValues.Area, fCalculateMedian([Area],[Price]) AS Median
-
FROM tblValues
-
ORDER BY tblValues.Area;
- Function Definition
- Public Function fCalculateMedian(strArea As String, curPrice As Currency)
-
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MySQL As String
-
Dim intNumOfRecords As Integer, curPriceValue As Currency
-
-
MySQL = "SELECT tblValues.Area, tblValues.Price FROM tblValues "
-
MySQL = MySQL & "WHERE tblValues.Area='" & strArea & "' ORDER BY tblValues.Area, tblValues.Price;"
-
-
Set MyDB = CurrentDb()
-
Set MyRS = MyDB.OpenRecordset(MySQL, dbOpenSnapshot)
-
-
MyRS.MoveLast: MyRS.MoveFirst
-
-
intNumOfRecords = MyRS.RecordCount
-
-
If intNumOfRecords = 0 Then
-
fCalculateMedian = Null
-
Exit Function
-
End If
-
-
If intNumOfRecords Mod 2 = 0 Then 'Even number of Records
-
MyRS.Move (intNumOfRecords \ 2) - 1 'Move half-way point
-
curPriceValue = MyRS![Price] '1st value to average
-
MyRS.MoveNext
-
curPriceValue = curPriceValue + MyRS![Price] '2nd value to average added to 1st value
-
fCalculateMedian = Format$(curPriceValue / 2, "Currency") 'Average them out
-
Else 'Odd number of Records
-
MyRS.Move (intNumOfRecords \ 2)
-
fCalculateMedian = Format$(MyRS![Price], "Currency")
-
End If
-
-
MyRS.Close
-
End Function
- Values in tblValues
- Area Price
-
California $100,000.00
-
California $150,000.00
-
California $120,000.00
-
New York $110,000.00
-
New York $20,000.00
-
New York $150,000.00
-
New York $120,000.00
-
Philadelphia $300,000.00
-
Philadelphia $100,000.00
-
Philadelphia $200,000.00
-
Philadelphia $116,000.00
-
Philadelphia $90,000.00
-
California $38,000.00
-
Trenton $0.00
- Query results
- Area Median
-
California $110,000.00
-
New York $115,000.00
-
Philadelphia $116,000.00
-
Trenton $0.00
P.S. - There is another alternative if you are interested and that is to let Excel do the work by passing an Array to its MEDIAN() Function.
4 3934
Subscribing, will return later.
I found various routines when searching using "access median function".
The only additional thing for you is the filtering of the data for one product, thus you'll have to add the productID to the parameters of the function and the WHERE clause in the strSQL.
Getting the idea ?
Nic;o)
Here is a little Function that I created that is used within the context of a Calculated Field in a DISTINCT Query. In this particular case, it accepts two Arguments: Distinct Field and Field to calculate the actual Median Value. In this specific instance, I calculated the Median Value for properties by Area. I'll post the SQL, Function Definition, Sample Data, and Query Execution results for clarification. - SQL Statement
- SELECT DISTINCT tblValues.Area, fCalculateMedian([Area],[Price]) AS Median
-
FROM tblValues
-
ORDER BY tblValues.Area;
- Function Definition
- Public Function fCalculateMedian(strArea As String, curPrice As Currency)
-
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MySQL As String
-
Dim intNumOfRecords As Integer, curPriceValue As Currency
-
-
MySQL = "SELECT tblValues.Area, tblValues.Price FROM tblValues "
-
MySQL = MySQL & "WHERE tblValues.Area='" & strArea & "' ORDER BY tblValues.Area, tblValues.Price;"
-
-
Set MyDB = CurrentDb()
-
Set MyRS = MyDB.OpenRecordset(MySQL, dbOpenSnapshot)
-
-
MyRS.MoveLast: MyRS.MoveFirst
-
-
intNumOfRecords = MyRS.RecordCount
-
-
If intNumOfRecords = 0 Then
-
fCalculateMedian = Null
-
Exit Function
-
End If
-
-
If intNumOfRecords Mod 2 = 0 Then 'Even number of Records
-
MyRS.Move (intNumOfRecords \ 2) - 1 'Move half-way point
-
curPriceValue = MyRS![Price] '1st value to average
-
MyRS.MoveNext
-
curPriceValue = curPriceValue + MyRS![Price] '2nd value to average added to 1st value
-
fCalculateMedian = Format$(curPriceValue / 2, "Currency") 'Average them out
-
Else 'Odd number of Records
-
MyRS.Move (intNumOfRecords \ 2)
-
fCalculateMedian = Format$(MyRS![Price], "Currency")
-
End If
-
-
MyRS.Close
-
End Function
- Values in tblValues
- Area Price
-
California $100,000.00
-
California $150,000.00
-
California $120,000.00
-
New York $110,000.00
-
New York $20,000.00
-
New York $150,000.00
-
New York $120,000.00
-
Philadelphia $300,000.00
-
Philadelphia $100,000.00
-
Philadelphia $200,000.00
-
Philadelphia $116,000.00
-
Philadelphia $90,000.00
-
California $38,000.00
-
Trenton $0.00
- Query results
- Area Median
-
California $110,000.00
-
New York $115,000.00
-
Philadelphia $116,000.00
-
Trenton $0.00
P.S. - There is another alternative if you are interested and that is to let Excel do the work by passing an Array to its MEDIAN() Function.
Thank you! That did the trick.
You have solved the challenge :=)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Hugo L. |
last post by:
I really don't know how to calculate the median. Can anybody help me?
|
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
|
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...
|
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...
|
by: brady |
last post by:
I have a function called int calcMedian(int ar, int numElements) How do i code the function to calculate the median in the array? Please help
|
by: CrostonScottish |
last post by:
Has anybody got any ideas or nifty code for calculating the median value in a form.
I currently have a database which we use for post-course evaluations. Part of the evaluation asks the attendees...
|
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...
|
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...
|
by: dmorand |
last post by:
Does anyone have any experience with retrieving a median value using SQL server? I'm trying to retrieve a median value from a list of various numerical values.
Any help would be appreciated.
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |