Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2004
    Posts
    5

    Unanswered: Date Range Expression and Averages

    Hi, All,

    I'm a bit stumped about expression/SQL syntax for what I need done, although it seems simple to me. I have one table, a master date table, which is every Monday. In an unrelated table, I have daily values. What I would like to do is run an expression or query on the daily value table such that it displays the average of the values for the week, with the corresponding result date being a Monday (so I can relate it to other "Monday only" tables). I hope this makes sense, and thanks for your help.

    EXAMPLES:
    master date table:
    date
    12/5/2005
    12/12/2005
    12/19/2005

    daily value table:
    date value
    12/5/2005 2
    12/6/2005 4
    12/7/2005 2
    12/9/2005 4
    12/13/2005 2

    result table:
    date value
    12/5/2005 3
    12/12/2005 2

  2. #2
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    What happens on 1/1/06? Do you need that handled seperately? You can use datepart("w", yourDate) to pull the week number and use it as a grouping level, but you have to do some additional work if you want to pull the same PHYSICAL week (ie: week 1 of january needs to be grouped seperate from week 1 of february).
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  3. #3
    Join Date
    Oct 2004
    Posts
    5
    Hi, Teddy,

    This solution needs to work across numerous years (I already have about forty in the daily set). Moreover, isn't that "w" a weekday parse? I'm not sure how that would help me, as a "2, 3, 4" result set I'd want grouped by that Monday's actual date (and the corresponding field values averaged).

    I may be missing something, though. Thanks.

  4. #4
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    Oh yes, good catch. "ww" will give you week number.

    Edit: Almost forgot, remember to use the optional "First Day of Week" parameter.
    Last edited by Teddy; 12-08-05 at 17:34.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  5. #5
    Join Date
    Feb 2004
    Location
    New Zealand
    Posts
    1,424
    Provided Answers: 8
    Here is Some Code that started life in a lotus 123 (only the mod 7 bit)
    I work out the Weekending of any Date
    my weekend is a saturday
    [code]
    Function WEEKEND(dat) As Date
    If IsNull(dat) Then Exit Function
    dat = DateSerial(Year(dat), Month(dat), Day(dat))
    If dat Mod 7 > 0 Then
    WEEKEND = dat - dat Mod 7 + 7
    Else
    WEEKEND = dat
    End If
    End Function
    [\code]

    satweekend = weekend(#anydatehere#)

    so a quick mod to my code to make it a monday end would should be

    [code]
    Function MondayEnd(dat) As Date
    If IsNull(dat) Then Exit Function
    dat = DateSerial(Year(dat), Month(dat), Day(dat))
    If dat Mod 7 > 0 Then
    MondayEnd = (dat - dat Mod 7 + 7) - 5
    Else
    MondayEnd = dat - 5
    End If
    End Function
    [\code]

    how to Use it

    paste code in a Module

    then in a query

    write

    MondayEnd:MondayEnd([adatefeild])

    this will create new col showing the Mondayend of the datefeild

    so by grouping it by the MondayEnd You Should be able to Sumby or average and get totals for that mondayend













    Quote Originally Posted by pjwoods
    Hi, All,

    I'm a bit stumped about expression/SQL syntax for what I need done, although it seems simple to me. I have one table, a master date table, which is every Monday. In an unrelated table, I have daily values. What I would like to do is run an expression or query on the daily value table such that it displays the average of the values for the week, with the corresponding result date being a Monday (so I can relate it to other "Monday only" tables). I hope this makes sense, and thanks for your help.

    EXAMPLES:
    master date table:
    date
    12/5/2005
    12/12/2005
    12/19/2005

    daily value table:
    date value
    12/5/2005 2
    12/6/2005 4
    12/7/2005 2
    12/9/2005 4
    12/13/2005 2

    result table:
    date value
    12/5/2005 3
    12/12/2005 2
    hope this help

    See clear as mud


    StePhan McKillen
    the aim is store once, not store multiple times
    Remember... Optimize 'til you die!
    Progaming environment:
    Access based on my own environment: DAO3.6/A97/A2000/A2003/A2007/A2010
    VB based on my own environment: vb6 sp5
    ASP based on my own environment: 5.6
    VB-NET based on my own environment started 2007
    SQL-2005 based on my own environment started 2008
    MYLE
    YOUR PASSWORD IS JUST LIKE YOUR TOOTHBRUSH DON'T SHARE IT.

  6. #6
    Join Date
    Feb 2004
    Location
    New Zealand
    Posts
    1,424
    Provided Answers: 8
    This got me thinking if I change it to this to

    Code:
    Function MyWeekEndDate(dat) As Date
    Dim Daydiff
    If IsNull(dat) Then Exit Function
    '********************************
    ' This Will return a Weekending Date
    ' Must set the Daydiff =
    ' 0 = Sat , 6 = Sun
    ' 5 = Mon , 4 = Tue
    ' 3 = Wed , 2 = Thu
    ' 1 = Fri
    ' Setting Daydiff to to a Above day Should Return that Date in
    ' the week in question
    '********************************
    Daydiff = 1
    dat = DateSerial(Year(dat), Month(dat), Day(dat))
    If dat Mod 7 > 0 Then
    MyWeekEndDate = (dat - dat Mod 7 + 7) - Daydiff
    Else
    MyWeekEndDate = dat - Daydiff
    End If
    End Function
    Still Just need some body to test it.
    Last edited by myle; 12-08-05 at 19:45.
    hope this help

    See clear as mud


    StePhan McKillen
    the aim is store once, not store multiple times
    Remember... Optimize 'til you die!
    Progaming environment:
    Access based on my own environment: DAO3.6/A97/A2000/A2003/A2007/A2010
    VB based on my own environment: vb6 sp5
    ASP based on my own environment: 5.6
    VB-NET based on my own environment started 2007
    SQL-2005 based on my own environment started 2008
    MYLE
    YOUR PASSWORD IS JUST LIKE YOUR TOOTHBRUSH DON'T SHARE IT.

  7. #7
    Join Date
    Oct 2004
    Posts
    5
    gentlemen, you two are rockstars and myle's code works perfectly. of course, while the logic and code lines make perfect sense to me, VBA syntax is a bit alien to me, as i've never worked with it.

    that's another thread for another time.

    at any rate, thank you. this issue had stumped me on and off for a month.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •