473,385 Members | 1,693 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,385 software developers and data experts.

I am trying to get the Median for Groups within a Table

The database is set up with Mission and within each mission are sets. Each set contains aproxmently 150 records that date/time temp, depth. I want to find the median for both temp and depth for each set. I have used the function provided by the MS Access support page but it calculates the median for the whole data base not each set. How would I get it to sort the data before finding the median.
thanks
-Eric
Apr 8 '11 #1
7 1410
NeoPa
32,556 Expert Mod 16PB
Not very clear Eric (You should fix the typos before posting). What function are you using? What SQL have you tried?

I would expect you would need to GROUP BY whatever the sets are, but you haven't explained that properly so I have very little to work with.

I'm sure if you take the time to present the question fully and without errors we can help you to a solution.
Apr 9 '11 #2
Ok thanks

The database is of depth and temperature records.

The columns are named

Mission, Set#, DateAndTime, Temp, Depth

Each Mission contains many sets. Each set contains about 150 different records from every 12 seconds between the start and end time. I have created a module with the code from the MS Access help page…

Expand|Select|Wrap|Line Numbers
  1. Function Median(tName As String, fldName As String) As Single
  2.   Dim MedianDB As DAO.Database
  3.   Dim ssMedian As DAO.Recordset
  4.   Dim RCount As Integer, i As Integer, x As Double, y As Double, _
  5.       OffSet As Integer
  6.   Set MedianDB = CurrentDb()
  7.   Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
  8.             "] FROM [" & tName & "] WHERE [" & fldName & _
  9.             "] IS NOT NULL ORDER BY [" & fldName & "];")
  10.     ssMedian.MoveLast
  11.   RCount% = ssMedian.RecordCount
  12.   x = RCount Mod 2
  13.   If x <> 0 Then
  14.      OffSet = ((RCount + 1) / 2) - 2
  15.      For i% = 0 To OffSet
  16.         ssMedian.MovePrevious
  17.      Next i
  18.      Median = ssMedian(fldName)
  19.   Else
  20.      OffSet = (RCount / 2) - 2
  21.      For i = 0 To OffSet
  22.         ssMedian.MovePrevious
  23.      Next i
  24.      x = ssMedian(fldName)
  25.      ssMedian.MovePrevious
  26.      y = ssMedian(fldName)
  27.      Median = (x + y) / 2
  28.   End If
  29.   If Not ssMedian Is Nothing Then
  30.      ssMedian.Close
  31.      Set ssMedian = Nothing
  32.   End If
  33.   Set MedianDB = Nothing
  34. End Function
When I run this it gives me the median of the either database. I was hoping to be able to create a module that would allow me to find the median of each set.
Apr 11 '11 #3
TheSmileyCoder
2,322 Expert Mod 2GB
The basic principle is to modify your SQL statement within the function.

You would need to make 2 modifications to your described function. First make room in the function calling to accept the Set Nr, and mission NR (assuming its a nr)
Expand|Select|Wrap|Line Numbers
  1. Function Median(tName As String, fldName As String,lngMissionNr as long, lngSetNr as long) As Single
Then modify the SQL's where clause.
Expand|Select|Wrap|Line Numbers
  1. SELECT [" & fldName & "]" & _
  2.             " FROM [" & tName & "] " & _
  3.             " WHERE [" & fldName & "] IS NOT NULL " & _
  4.             " AND Mission=" & lngMissionNr & _
  5.             " AND [SET#]=" & lngSetNr & _
  6.             "ORDER BY [" & fldName & "];"
Apr 11 '11 #4
ADezii
8,834 Expert 8TB
Wouldn't you have to GROUP BY Mission and Set#, then Calculate the Medians for both Temp and Depth?
Apr 11 '11 #5
I tried to use the
[SELECT [" & fldName & "]" & _]
but the module did not like it and marked it as red.
Apr 11 '11 #6
TheSmileyCoder
2,322 Expert Mod 2GB
An answer like that gives NOTHING to work forward from. You have not shown HOW you use it, you only post a fraction of the code, so I have no idea whether its because you simply do not grasp the concept, or was to lazy to post it. You enclose it in square brackets which makes no sense to me as a reader. Come back and try again.
Apr 11 '11 #7
NeoPa
32,556 Expert Mod 16PB
I think the confusion here Smiley (and Eric) is that post #4 has code that starts in the middle of a line of VBA. Most of us that do this a lot would probably have recognised that as a simple oversight, but I expect Eric was confused by it and tried to make some sense out of it, but with little experience of the subject failed to see what was going on.

Smiley's code should probably have started something like :
Expand|Select|Wrap|Line Numbers
  1.    strSQL = "SELECT [" & fldName & "]" & _
  2.             " FROM [" & tName & "] " & _
  3.             ...
NB. In Access one often has to work with SQL strings, and this gets particularly complicated when creating SQL strings from within VBA code (as in this instance). I hope this clarifies the situation somewhat :-)
Apr 12 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jim west via SQLMonster.com | last post by:
I have website problems with mySQL data base, I have a website and was told the only way to change my password is within my SQL data base files and that it would be over my head buy the site maker,...
2
by: narasingarao | last post by:
Hi to group, I'm a student of M.C.A. from B.I.T. Ranchi...I'm in my project period here i have to migrate the MS-Access database table to Oracle data base tables...so, please help me in getting...
4
by: Paul Copeland | last post by:
Hi, I am using Access2000 and have developed a data base which successfully uses DAO code in some of the forms to update tables. Recently I converted the data base to a split MDE data base and now...
2
by: Vicente Nicolau | last post by:
Hello I'm making a PDA project that uses a data base. That data base is saved in a xml file. When the application starts up, I load the xml file in memory. The application makes changes in the...
7
by: Lian | last post by:
Hi every body I have a problem with accesses and visual studio. I've just installed XP and I can't get a connection with the Data base. I have this error and I don't know how to fix it The...
4
by: AI Man | last post by:
Greetings, I have been searching the web and Access relentlessly trying to find a way to call an Access macro from a different Access data base. Example - The form button gets clicked and the...
3
by: nandhanvijay | last post by:
hi every am trying to connect data base in vb.net but am unable to connect. As per the book am following that says to connect to data base go to tools menu connect to data base option. u will...
24
by: joeldault | last post by:
Question For Microsoft Access Data Base -------------------------------------------------------------------------------- I am Trying to create a single formula that would do the following: If...
0
by: apwuhp | last post by:
We have used a data replication product that has replicated our production data base to another server, both servers are AIX 5.3 operating system and IBM DB2 data bases. The product replicates at the...
1
by: gerryis2000 | last post by:
Hi, kindly guide me on how to solve this security problem. i set a passord to the data base and seems to work well. but since several people use the data base and know the passord, i think its risky....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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...
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...

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.