Results 1 to 7 of 7
  1. #1
    nvrwrkn is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2012
    Posts
    35

    Find highest 4 hours out of 24

    Howdy!



    I have a need to find the highest 4 consecutive hours out of 24, each hour field containing some value.

    Output would look like 0400 to 0759 or 0500 to 0859.

    Trying to find the largest 4 hour block of hours in a day where so many values occur during each hour.

    I'm thinking something that has to do 24 groups, 1st group is 00 to 04, second 01 to 05, etc and the calc the total values found in each block, but I can't get my head around how to do it.

    Any helps is appreciated!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,943
    Use the TOP keyword.

    Review
    http://allenbrowne.com/subquery-01.html
    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.

  3. #3
    nvrwrkn is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2012
    Posts
    35
    sorry... I've been off for a few days.

    As for the TOP keyword, my useage of it so far has been to find the TOP N in a sort. Unless I'm not seeing deeper into your meaning, using TOP will just show me the top 4 hours with the largest value in each hour. That could be 0400, 1700, 2100, 2200. What I was looking to do was find the largest consecutive block of 4 hours... basically like taking a look at the total for 00 to 04, then 01 to 05, then 02 to 06 and so on and then finding which of those consecutive 4 hour blocks has the largest combined values.

    Did I not look far enough into what you meant using TOP?

    As I write this I'm thinking I'm gonna have to do something ugly to accomplish this, like a ton of hitting the tables.

    Can this be manipulated in recordsets somehow? Can I do this creating 24 recordsets using each 4 hour block or is there another way to look at the entire table 4 consecutive chunks at a time and advancing the starting block by 1 each time until I hit the 24th block?

    Here's an example... if the chart's big enough (ok, it's LARGE), you can see that 06,07,08,09 is the largest block of 4 hours.
    Click image for larger version. 

Name:	Image1.jpg 
Views:	7 
Size:	49.4 KB 
ID:	8096

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,943
    Provide sample data for analysis. An Excel might be sufficient.
    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.

  5. #5
    nvrwrkn is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2012
    Posts
    35
    Table1.xls.zip


    If I attached correctly the excel inside produces a similar chart based on HOUR, and a count of CountofData. The chart produced shows hours 06 to 09 with a total count of 16, making that 4 hour block the largest out of all other possible 4 hour blocks.

    Basically the need is to find a 4 hour consecutive block of time where the most events ( or COUNT of data ) happen.

    *not at work now so I'm just making up something quick for a visual

    *another edit: it's very likely that there may be times when an hour is not represented in the table, meaning no event was found during that hour and there was no datacount entered for that particular hour.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,943

    Highest sum of rolling 4 consecutive records

    I think this will require VBA code using recordset, arrays, and looping structures. Something like:
    Code:
    Option Compare Database
    Option Explicit
    Option Base 1
    
    Sub GetSum()
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    Dim dblSum As Double, i As Integer
    Dim ary4(4), aryD()
    rs.Open "SELECT * FROM Table1 ORDER BY ID;", CurrentProject.Connection, adOpenDynamic, adLockPessimistic
    For i = 1 To 4
        ary4(i) = rs!CountOfData
        rs.MoveNext
    Next
    ReDim aryD(1)
    While Not rs.EOF
        aryD(UBound(aryD)) = ary4(1) + ary4(2) + ary4(3) + ary4(4)
        ary4(1) = ary4(2)
        ary4(2) = ary4(3)
        ary4(3) = ary4(4)
        ary4(4) = rs!CountOfData
        rs.MoveNext
        ReDim Preserve aryD(UBound(aryD) + 1)
    Wend
    dblSum = aryD(1)
    For i = 1 To UBound(aryD)
        If aryD(i) > dblSum Then dblSum = aryD(i)
    Next
    Debug.Print dblSum
    End Sub
    This should give you some idea of the difficulties involved.
    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.

  7. #7
    nvrwrkn is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2012
    Posts
    35
    sigh....time to study up on arrays then. Figured the fastest was a recordset but never had a need to use arrays before, so now it's back to the books.

    Thanks much!

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

Similar Threads

  1. Selecting highest value across several fields
    By megabrown in forum Queries
    Replies: 19
    Last Post: 10-28-2014, 01:16 AM
  2. Showing highest value
    By Maikelos27 in forum Queries
    Replies: 1
    Last Post: 03-09-2012, 08:40 AM
  3. Find 2nd or 3rd highest record with a query
    By K Roger in forum Queries
    Replies: 1
    Last Post: 12-19-2011, 11:27 PM
  4. Select 5 highest values
    By frozendmj in forum Queries
    Replies: 1
    Last Post: 04-19-2011, 03:18 PM
  5. query to find the highest value in a field
    By jhjarvie in forum Queries
    Replies: 0
    Last Post: 09-02-2008, 02:27 PM

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