Results 1 to 8 of 8
  1. #1
    HectorH is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    14

    Efficient way to return the average based on condition and day/time

    Hello all,



    I got a problem that I have a solution to but it is inefficient and I believe there is an easier way but it alludes me. Here it is:

    I have two tables, one has equipment information i.e. size, the other contains hourly reads for each piece of equipment for a month duration. so 744 reads per equipment.

    I want to get the day of the week averages for each piece of equipment that meets the size criteria. so For mondays I want the avr result from a "Large" equipment for Mon, tues Wed Thurs, friday.

    What I did was create a query that brings in the size field so I just have one table than I use the davg function for each day. Code is very short but runtime is long.

    Any suggestions

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    D functions (dsum, davg, dmin, etc) are very inefficient especially over larger data sets.

    as I understand it you want to see the average of all the mondays in a given month, an average for all the tuesdays in a given month, etc

    What I would do in this case is use the weekday([DateField]) function. It will return a numeric value for the day of the week. Sunday Being 1, Saturday being 7. You can alter this by expressly telling the weekday which day to treat as 1 by inserting the number of the day you want to start in, for instance if you want to return a 1 for monday you would have

    Weekday([DateField], 2)

    Then you can average all of you data on a particular work day without using the davg function.

  3. #3
    HectorH is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    14
    I am actually using that within the dAVG. I just don't know how to get the avg of each day with the SIZE criteria from the other table being met. I can do it for one day but when I do it for multiple days it bugs out. I thought about using some sort of variable representing each column/day variables and using seven different subqueries but had no luck. After this I want to do the same query but with time of day. Get the 1pm average thru out the month.

    Should look like this:

    Monday AVG ,Tues AVG,....
    10 15

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I don't know what your original dataset looks like but if your dates are in a normalized table what you're looking at with your desired output is a crosstab query.

    If you can post an example of your starting data and what you want the output to be it would be easier to point you in the right direction.

  5. #5
    HectorH is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    14
    Crosstab, I've never used that before, I'll read up on it. I won't be working on that project for the next few days but I appreciate your input. At the very least I will have something to look into so thank you.

  6. #6
    HectorH is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    14
    So Im still stuck!

    Here is the data set table

    Read, Day stamp , Time Stamp, Size
    10, 5, 5/10/11, 0:00, Large (example row)
    This is how the data is laid out in a table.

    I want the average reads by size for Mondays,....Sunday
    I want the size, avg for mon, avg for tues, ...as columns
    One row will show the size, and averages for each day of the week

    Here is sQl to get just one size, I have various sizes
    select a.[SIZE],avg(Read) AS Monday
    from [Reads by Size] a where Weekday (Day, 2 )=1
    group by a.[SIZE]
    Now I need to add Tuesday average colum for each size and so on.

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    You have four column (read, day stamp, time stamp, size) headings but five columns of data if you are separating by commas.

    What does the column READ represent, the number of times something is measured or is it a primary key or what?

    Let's say you had this data set (for this example I'm assuming the second column of your 'data' is just the day of the month and I am ignoring it), I am ignoring the time stamp because it's irrelevant to what you've asked as well.

    Code:
    Read DayStamp Size
    10   5/10/11  Large
    10   5/11/11  Large
    11   5/9/11   Large
    11   5/10/11  Large
    What do you want the output to be?

    Code:
    Read Size   Monday Tuesday Wednesday ...
    10   Large         1       1
    11   Large  1      1

  8. #8
    HectorH is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    14
    I had a Eureka moment yesterday and answered my on question thanks for the help.

    FYI Select sum(Mon) as Monday, sum (Tues).....
    from ( Select avg(Read),0,0,0,0,0 from table where weekday(readdate,2)=1 group by size union Select 0, avg(Read),0,0.....)
    group by Size

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

Similar Threads

  1. Printing multiple reports at once based on condition
    By justinwright in forum Reports
    Replies: 24
    Last Post: 04-13-2011, 01:40 PM
  2. Replies: 1
    Last Post: 08-04-2010, 04:38 PM
  3. Update 2 fields based on where condition.
    By Confused in forum Access
    Replies: 2
    Last Post: 11-19-2009, 05:21 PM
  4. How do I group & average at the same time?
    By B20Pete in forum Queries
    Replies: 1
    Last Post: 07-07-2009, 12:53 PM
  5. Highlighting Report Data Based on a Condition
    By KramerJ in forum Reports
    Replies: 1
    Last Post: 05-29-2009, 10:27 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