Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2008

Tip: Looking for answers? Try searching our database.

Do...Loop Help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
chucklloyd - 10 Mar 2008 16:19 GMT
I have a table that has data based on 15 minute increments.  The table has an
ID field, a time field, and a total field.  I would like to identify the
highest total hour for the data which typical spans 12 hours.  How do I
create the loop to evaluate the total for four 15 minute increments, store
the value and then evaluate the next total fo four 15 minute increments.  The
output would ideally identify the range of the 4 15 minute increments that
have the highest total.
John Spencer - 10 Mar 2008 17:13 GMT
So, does ID identify a specific record or is it an identifier for a specific
group of records?

Do you have more than one period of time (more than  one day) in your table?

Does your time field only have the time in it or does it also include the
date?

Perhaps this will get you started.  It assumes only times in the field and
only one days worth of data

SELECT A.YourTimeField, Sum(B.TotalField) as RollingTotal
FROM A.YourTable = B.YourTable
WHERE A.YourTimeField >= B.YourTimeField
AND A.YourTimeField <= DateAdd("n",65,B.YourTimeField)
GROUP BY A.YourTimeField

SELECT Top 1 Q.YourTimeField, RollingTotal
FROM TheAboveQuery as Q
ORDER BY RollingTotal Desc

If you care to post the actual field and table names, perhaps that can all
be rolled up into one query.  Also answer the questions about your data.

Signature

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
.

>I have a table that has data based on 15 minute increments.  The table has
>an
[quoted text clipped - 5 lines]
> output would ideally identify the range of the 4 15 minute increments that
> have the highest total.
chucklloyd - 10 Mar 2008 18:27 GMT
My table has the following fields
TrafficCountID (unique values for each record)
StartTime (6:00,6:15,6:30....)
Total (integer)

It is only for a 12 hour period, or 48 records.
Just the time in 15 minute increments

> So, does ID identify a specific record or is it an identifier for a specific
> group of records?
[quoted text clipped - 29 lines]
> > output would ideally identify the range of the 4 15 minute increments that
> > have the highest total.
John Spencer - 10 Mar 2008 20:34 GMT
SELECT Top 1 Q.StartTime, Q.RollingTotal
FROM
(
SELECT A.StartTime, Sum(B.Total) as RollingTotal
FROM YourTable as A INNER JOIN YourTable As B
ON A.StartTime>= B.StartTime
AND A.StartTime<= DateAdd("n",65,B.StartTime)
GROUP BY A.StartTime
) as Q
ORDER BY RollingTotal Desc

--Copy the above SQL statement.
--Open a new query
-- Don't add any tables when asked, just close the dialog
-- Select VIEW : SQL from the menu
-- Paste the above into the window.
-- Replace YourTable with the name of your table.

I don't know what I was thinking of earlier, but somehow I really, really
messed up the query I posted.
Signature

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
.

> My table has the following fields
> TrafficCountID (unique values for each record)
[quoted text clipped - 46 lines]
>> > that
>> > have the highest total.
George Nicholson - 11 Mar 2008 16:27 GMT
Very, very slick John :-)

Signature

HTH,
George

> SELECT Top 1 Q.StartTime, Q.RollingTotal
> FROM
[quoted text clipped - 68 lines]
>>> > that
>>> > have the highest total.
chucklloyd - 11 Mar 2008 20:49 GMT
Works like a champ.  
Thanks so much for your help.

> Very, very slick John :-)
>
[quoted text clipped - 70 lines]
> >>> > that
> >>> > have the highest total.
George Nicholson - 10 Mar 2008 17:43 GMT
When you say highest total hour, can that cross hh:00? (i.e., 12:30 am,
12:45 am, 1:00 am, 1:15 am, making 12x4 total groupings) or do you just want
to evaluate all hh:00, hh:15, hh:30, hh:45 groupings as 12 separate
entities?

If the former, you'd need a fair amount of code. I might create an array to
hold all 48 values and then a loop to total each group of 4, comparing the
Current4Total to the previous High4Total and storing High4Time in a variable
each time a new High4Total is identified.  Probably an easier way, that's
just off the top of my head.

If the latter, you can simply create a totals query, sorted Descending, that
only returns the Top record:

Something like:

SELECT TOP 1 Hour([MyDateTime]) AS FullHour, Sum([MyTotal]) AS TotalValue
FROM MyTable
GROUP BY Hour([MyDateTime])
ORDER BY Sum([MyTotal]) DESC;

Signature

HTH,
George

>I have a table that has data based on 15 minute increments.  The table has
>an
[quoted text clipped - 5 lines]
> output would ideally identify the range of the 4 15 minute increments that
> have the highest total.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.