-----Original Message-----
Todd said:
This one has me stumped.
I have a table which has this basic structure.
Equipment#,Status,StatusDate
xxx,stat1,2/1/03
yyy,stat2,1/3/03
xxx,stat2,1/4/03
xxx,stat1,1/3/02
yyy,stat1,2/2/03
xxx,stat1,4/2/04
yyy,stat1,2/2/04
What I want to do is to create a query to return the time
between status events (CycleTime) for a particular piece
of equipment.
So the resulting query for the example above would look
like this:
Equipment#,status,StatusDate,Next:Status Date,CycleTime
xxx, stat1, 1/3/02, 2/1/03, 393 days
xxx, stat1, 2/1/03, 4/2/03, 61 days
xxx, stat1, 4/2/03, null, null
xxx, stat2, 1/4/03, null, null
yyy, stat1, 2/2/03, 2/2/04, 365 days
yyy, stat2, 1/3/03, null, null
How would I do this? My stumbling block is trying to
figure out how to identify the "Next" status date and
return it.
Hi Todd,
I'm sure Brian's solution is probably right on,
so with apologies to Brian for butting in,
but maybe a different way
at looking through the process...
assumed "tblStatus" as name of your table
and sorted them by "E, S, SD"
Equipment# Status StatusDate nextwouldbe
xxx stat1 1/3/2002 2/1/2003
xxx stat1 2/1/2003 4/2/2004
xxx stat1 4/2/2004
xxx stat2 1/4/2003
yyy stat1 2/2/2003 2/2/2004
yyy stat1 2/2/2004
yyy stat2 1/3/2003
so it was easier to see what your
"next" would be.
Started with a simple query where
brought the table in twice.
SELECT
t1.[Equipment#],
t1.Status,
t1.StatusDate AS t1StatusDate,
t2.StatusDate AS t2StatusDate,
[t2].[StatusDate]-[t1].[StatusDate] AS CycleTime
FROM tblStatus AS t1 INNER JOIN tblStatus AS t2
ON (t1.[Equipment#] = t2.[Equipment#])
AND (t1.Status = t2.Status)
ORDER BY t1.[Equipment#],
t1.Status, t1.StatusDate, t2.StatusDate;
Equipment# Status t1StatusDate t2StatusDate CycleTime
xxx stat1 1/3/2002 1/3/2002 0
xxx stat1 1/3/2002 2/1/2003 394
xxx stat1 1/3/2002 4/2/2004 820
xxx stat1 2/1/2003 1/3/2002 -394
xxx stat1 2/1/2003 2/1/2003 0
xxx stat1 2/1/2003 4/2/2004 426
xxx stat1 4/2/2004 1/3/2002 -820
xxx stat1 4/2/2004 2/1/2003 -426
xxx stat1 4/2/2004 4/2/2004 0
xxx stat2 1/4/2003 1/4/2003 0
yyy stat1 2/2/2003 2/2/2003 0
yyy stat1 2/2/2003 2/2/2004 365
yyy stat1 2/2/2004 2/2/2003 -365
yyy stat1 2/2/2004 2/2/2004 0
yyy stat2 1/3/2003 1/3/2003 0
To that query I added one more field ( a select
subquery where find the minimum date that is
larger than t1 date)
SELECT
t1.[Equipment#],
t1.Status,
t1.StatusDate AS t1StatusDate,
t2.StatusDate AS t2StatusDate,
[t2].[StatusDate]-[t1].[StatusDate] AS CycleTime,
(SELECT Min(t3.StatusDate) From tblStatus As t3
WHERE t3.[Equipment#]=t1.[Equipment#]
AND t3.Status=t1.Status A
ND t3.StatusDate>t1.StatusDate) AS NextStatusDate
FROM tblStatus AS t1 INNER JOIN tblStatus AS t2
ON (t1.[Equipment#] = t2.[Equipment#]) AND (t1.Status = t2.Status)
ORDER BY t1.[Equipment#], t1.Status, t1.StatusDate, t2.StatusDate;
producing:
"E" "S" t1StatusDate t2StatusDate CycleTime NextStatusDate
xxx stat1 1/3/2002 1/3/2002 0 2/1/2003
xxx stat1 1/3/2002 2/1/2003 394 2/1/2003
xxx stat1 1/3/2002 4/2/2004 820 2/1/2003
xxx stat1 2/1/2003 1/3/2002 -394 4/2/2004
xxx stat1 2/1/2003 2/1/2003 0 4/2/2004
xxx stat1 2/1/2003 4/2/2004 426 4/2/2004
xxx stat1 4/2/2004 1/3/2002 -820
xxx stat1 4/2/2004 2/1/2003 -426
xxx stat1 4/2/2004 4/2/2004 0
xxx stat2 1/4/2003 1/4/2003 0
yyy stat1 2/2/2003 2/2/2003 0 2/2/2004
yyy stat1 2/2/2003 2/2/2004 365 2/2/2004
yyy stat1 2/2/2004 2/2/2003 -365
yyy stat1 2/2/2004 2/2/2004 0
yyy stat2 1/3/2003 1/3/2003 0
it looks like for any Equipment#/Status/tStatusDate,
it is selecting the correct "NextStatusDate."
Next, changed query to filter out only the records where
t2StatusDate = NextStatusDate
and saved as "qryStatus"
SELECT
t1.[Equipment#], t1.Status,
t1.StatusDate,
(SELECT Min(t3.StatusDate) From tblStatus As t3
WHERE t3.[Equipment#]=t1.[Equipment#]
AND t3.Status=t1.Status
AND t3.StatusDate>t1.StatusDate) AS NextStatusDate,
[t2].[StatusDate]-[t1].[StatusDate] AS CycleTime
FROM tblStatus AS t1 INNER JOIN tblStatus AS t2
ON (t1.[Equipment#] = t2.[Equipment#]) AND (t1.Status = t2.Status)
WHERE ((((SELECT Min(t3.StatusDate) From tblStatus As t3
WHERE t3.[Equipment#]=t1.[Equipment#]
AND t3.Status=t1.Status
AND t3.StatusDate>t1.StatusDate))=[t2].[StatusDate]))
ORDER BY t1.[Equipment#], t1.Status, t1.StatusDate;
qryStatus produced:
Equipment# Status StatusDate NextStatusDate CycleTime
xxx stat1 1/3/2002 2/1/2003 394
xxx stat1 2/1/2003 4/2/2004 426
yyy stat1 2/2/2003 2/2/2004 365
close..but one more query:
SELECT
tblStatus.[Equipment#],
tblStatus.Status,
tblStatus.StatusDate,
qryStatus.NextStatusDate,
qryStatus.CycleTime
FROM tblStatus LEFT JOIN qryStatus
ON (tblStatus.StatusDate = qryStatus.StatusDate)
AND (tblStatus.Status = qryStatus.Status)
AND (tblStatus.[Equipment#] = qryStatus.[Equipment#])
ORDER BY tblStatus.[Equipment#], tblStatus.Status, tblStatus.StatusDate;
producing:
Equipment# Status StatusDate NextStatusDate CycleTime
xxx stat1 1/3/2002 2/1/2003 394
xxx stat1 2/1/2003 4/2/2004 426
xxx stat1 4/2/2004
xxx stat2 1/4/2003
yyy stat1 2/2/2003 2/2/2004 365
yyy stat1 2/2/2004
yyy stat2 1/3/2003
Again, apologies for butting in.
Sometimes it has helped me when
posters have broken down solutions
like the above. Maybe it will help you.
Good luck,
Gary Walter
.