David:
If the rows aren't all paired there'll be no problem provided there is a way
you can identify each pair and each unpaired row. If there's a column
equivalent to EventID in my example, then its simple:
SELECT EventID,
DATEDIFF( "n",MIN(EventDateTime),
(SELECT MAX(EventDateTime)
FROM Events as E2
WHERE E2.EventID = E1.EventID))
AS TimeDifference
FROM Events As E1
GROUP BY EventID
HAVING COUNT(*) = 2;
This would simply ignore the unpaired rows.
SELECT EventID,
DATEDIFF( "n",MIN(EventDateTime),
(SELECT MAX(EventDateTime)
FROM Events as E2
WHERE E2.EventID = E1.EventID
GROUP BY EventID
HAVING COUNT(*) = 1))
AS TimeDifference
FROM Events As E1
GROUP BY EventID
This would return a Null elapsed time for each unpaired row.
Without an equivalent to EventID its possible that you might be able to use
the date/time data itself to identify the pairs and singletons, and thus fill
an EventID column, but that would depend on there being some pattern to the
data which would enable this.
Another strategy, which again depends on the presence of an EventID column
to identify the pairs, or the ability to fill one with an update query, would
be to add a StopDateTime column to the table and then fill it with:
UPDATE Events
SET StopDateTime =
DMAX("EventDateTime",
"Events","EventID = " & EventID);
You could then delete the redundant rows by running the following query:
DELETE *
FROM Events AS E1
WHERE EventdateTime = StopDateTime
AND (SELECT COUNT(*)
FROM Events As E2
WHERE E2.EventID = E1.EventID) = 2;
And rename the EventDateTime column by, either manually in table design
view or by adding the following function to a module in the database:
Public Sub RenameColumn_DAO(strTable As String, strOldName As String,
strNewName As String)
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(strTable)
Set fld = tdf.Fields(strOldName)
fld.Name = strNewName
End Sub
And calling it like so:
RenameColumn_DAO "Events","EventDateTime","StartDateTime"
This table is a better design as it now has separate columns for the start
and stop times, which is as it should be as these are separate attribute
types of the Events entity type. Computing the elapsed time is then simply a
case of subtracting the start from stop times with:
SELECT EventID,
DATEDIFF("n", StartDateTime, StopDateTime)
AS ElapsedTime;
BTW I know Scarborough quite well; one of my sons was a student at Hull
University's college there.
Ken Sheridan
Stafford, England