The query string I posted was an entire query. The subquery would have
been
just this portion, which you should enter into a new field
(SELECT Max[Work Date] as Prior
FROM [YourTable] As TB
WHERE TB.[Serial#] = [YourTable].[Serial#]
AND TB.[Work Date] < [YourTable].[Work Date] )
If you wanted to use the entire query as posted, you would need to edit
it
to use your tablename and field names and then paste it into a NEW query.
-- New Query
-- Add no tables
-- Select View SQL
-- Paste the SQL statement into the window
-- Attempt to run the statement
SELECT [Serial#], [Item Number], [Work Date]
, DateDiff("d", (SELECT Max[Work Date] as Prior
FROM [YourTable] As TB
WHERE TB.[Serial#] = TA.[Serial#]
AND TB.[Work Date] < TA.[Work Date]
) , [Work Date]) as ElapsedDays
FROM [YourTable] as TA
--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
PD said:
John,
I have not been able to figure out how to insert the statement as a sub
query, any suggestions?
Thanks,
--
PRD
:
One method would be to use a correlated subquery.
SELECT [Serial#], [Item Number], [Work Date]
, DateDiff("d", (SELECT Max[Work Date] as Prior
FROM [YourTable] As TB
WHERE TB.[Serial#] = TA.[Serial#]
AND TB.[Work Date] < TA.[Work Date]
) , [Work Date]) as ElapsedDays
FROM [YourTable] as TA
--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
Okay, the data I import is structure as so;
SERIAL# ITEM NUMBER WORK DATE
45612 P123456 12/7/06
45612 P123456 3/25/07
78913 P45215 1/5/04
78913 P45215 2/8/05
etc...
These are service records sorted by serial number, item number, and
work
date. I need to calculate the elapsed days between replacements of
like
item
numbers for each serial number.
Thanks a bunch
PD