Results 1 to 7 of 7
  1. #1
    Pusscat is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jun 2015
    Posts
    3

    Access 2013 calculate a running average in query design problems

    Hi,
    I have been battling with this little problem of mine, searching the internet and not quite getting the output I need and I’m looking for a little help and guidance please! I have a little experience with access but I’m getting nowhere fast…


    I have a table (see below) in Access 2013.
    URN Sample date B1 S1 S2 Log10B1
    73115166 10-May-12 973 500 185 2.9881
    73115166 17-May-12 10 500 185 1.0000
    73115166 23-May-12 10 500 185 1.0000
    73115166 27-May-12 45 500 185 1.6532
    73115166 07-Jun-12 18 500 185 1.2553
    73115166 11-Jun-12 155 500 185 2.1903
    73115166 15-Jun-12 10 500 185 1.0000
    73115166 20-Jun-12 10 500 185 1.0000

    Column 'URN' is short text (yep, i know it looks like numbers but they are text as some of these in future will have letters in them..), 'Sample date' is Date / time and the rest are numbers.
    The ‘Sample date’ entries are all unique and ‘URN’ are identical. The 'Log10B1' (numbers) may have replicates in them and the data is sorted from oldest to newest on 'Sample date'. The table with the data is called ‘Log Tables’.

    I’m trying to make a query to produce an output of the table and calculate a column which gives a running average (Runaverage) of the values in 'Log10B1' column, rather like the one below with the calculated field in yellow.
    URN Sample date B1 S1 S2 Log10B1 Runaverage
    73115166 10-May-12 973 500 185 2.9881 2.9881
    73115166 17-May-12 10 500 185 1.0000 1.994056
    73115166 23-May-12 10 500 185 1.0000 1.6627
    73115166 27-May-12 45 500 185 1.6532 1.6603
    73115166 07-Jun-12 18 500 185 1.2553 1.5793
    73115166 11-Jun-12 155 500 185 2.1903 1.6812
    73115166 15-Jun-12 10 500 185 1.0000 1.5838
    73115166 20-Jun-12 10 500 185 1.0000 1.5109

    I’ve tried variations of expressions in the Query design based on Davg([Log10B1],[Log Tables]) but I only get the average of the Log10B1 over the complete dataset. I’ve seen some ref to a Var(Davg….expression) with concatenation strings resent relating to variables (the ‘Sample date’?) but I’m at a loss where to start or what protocols. Can anyone help please?!

    Many thanks.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I am failing to see the pattern with your Runaverage. Also, I am unable to understand what you are trying to accomplish with Davg([Log10B1],[Log Tables]).

    Is there another way you can communicate the formula for Runaverage? What is Log Tables?

  3. #3
    IrogSinta is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Posts
    103
    You need to use a Nested query to accomplish this. Open a query in SQL view and paste the following in there:
    Code:
    SELECT T1.*, (SELECT Avg(T2.Log10B1) 
    FROM Table2 AS T2
    WHERE T2.[Sample date]<=T1.[Sample date]) AS RunAvg
    FROM Table2 AS T1
    ORDER BY T1.[Sample date]
    Ron

  4. #4
    Pusscat is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jun 2015
    Posts
    3
    Hi Ron,
    I pasted that in the SQL window but it didn't work.

    The table that holds the data is called 'Log Tables' . The SQL code you gave refers to T1, T2 and Table2.....do i need to change any of these in the SQL to the name 'Log Tables' and if so which ones please?
    Ta

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,920
    Change Table2 to [Log Tables].
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    IrogSinta is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Posts
    103
    Sorry I forgot to rename my test table (Table2) before I posted the code. I usually use NameOfTable in its place. So just as June7 said, rename Table2 to [Log Tables]. You need to include the brackets since you have a space in your table name. On a side note, it's preferable not to use spaces in your naming convention. Using either Underscores (Log_Tables), or CamelCase (LogTables) would be a better option.

    Ron

  7. #7
    Pusscat is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jun 2015
    Posts
    3
    Many thanks June and Ron, works a treat!

    Taken your advice and taken the spaces out of my table names as well. Love the internet for this sort of help and guidance, much appreciated!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 07-07-2014, 11:21 AM
  2. calculate the Average of certain records
    By mazides in forum Queries
    Replies: 1
    Last Post: 10-16-2013, 11:10 AM
  3. Access 2013 running very slow
    By razors1968 in forum Access
    Replies: 0
    Last Post: 08-01-2013, 01:31 PM
  4. Calculate the Average
    By Jerseynjphillypa in forum Queries
    Replies: 1
    Last Post: 06-18-2012, 03:26 PM
  5. Calculate average in a query
    By srbooth in forum Queries
    Replies: 1
    Last Post: 02-20-2010, 09:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums