Bring the table TWICE, one will get an _1 appended to its name.
Change the query type to a Total query.
Join the two references through their clientID field.
Bring all the required fields from the first reference in the grid. Keep
GROUP BY under clientID and under the dateTimeStamp field, use LAST, or
FIRST, for any other (optionally required) field.
Bring only the dateTimeStamp field for the second table reference in the
grid, the _1 one, and change the GROUP BY to WHERE, and, under it, add the
criteria:
< [tableName].[dateTimeStampFieldName]
(tableName without the _1 in the criteria)
So, now, you made _1 the whole history (in relation of the not_1 reference).
Make a computed expression in a new grid's column:
[tableName].[dateTimeStamp] - MAX( [tableName_1].[dateTimeStamp]
That should do the job, since MAX recuperate only the maximum value of the
'said' history.
If you prefer to do it in SQL view:
SELECT a.clientID,
a.dateTimeStamp,
LAST(a.someOtherField),
a.dateTimeStamp)-MAX(b.dateTimeStamp)
FROM tableName As a INNER JOIN tableName AS b
ON a.clientID = b.clientID
WHERE b.dateTimeStamp < a.dateTimeStamp
GROUP BY a.clientID, a.dateTimeStamp
Sure, that does not show records having no previous record (for that
client). Modify the SQL statement like below, to get those:
SELECT a.clientID,
a.dateTimeStamp,
LAST(a.someOtherField),
a.dateTimeStamp)-MAX(b.dateTimeStamp)
FROM tableName As a LEFT JOIN tableName AS b
ON a.clientID = b.clientID
AND b.dateTimeStamp < a.dateTimeStamp
GROUP BY a.clientID, a.dateTimeStamp
where the INNER JOIN has become a LEFT JOIN, and the where clause included
in the join. The expression a.dateTimeStamp)-MAX(b.dateTimeStamp) will
return null for records without previous one.
Vanderghast, Access MVP
Dave said:
I am trying to calculate how long it has been from a previous visit. I am
tracking visit dates for customers. So I am sorting visit dates by
customer.
I want to look at a record date and subtract the previous reocrds date
from
it for all dates in the db.
So in the end there is another Field that shows how long it has been
bewteen
customer visits?
Dave K