Well..I'm already "very late," so
here be a "less-dirty" solution:
In the first query we bring 2 instances
of your table, aliasing one as "CurrentData"
and the other as "NextData." Using 2 copies
of a table is acommon way for handling
values from "adjacent rows."
What makes a row from NextData
the adjacent (next) row to a row in
CurrentData?
- the ID's need to match up.
- the ADate for NextData needs
to be the earliest date in the table
that is later than current date.
SELECT
CurrentData.ID,
CurrentData.AValue,
CurrentData.ADate,
NextData.AValue AS NextValue
FROM tblStegman AS CurrentData
INNER JOIN tblStegman AS NextData
ON CurrentData.ID = NextData.ID
WHERE
NextData.ADate =
(SELECT Min(t.Adate)
FROM tblStegman As t
WHERE t.ID = CurrentData.ID
AND
t.ADate > CurrentData.ADate);
from your example data, we'd get:
ID AValue ADate NextValue
1 10 1/10/2003 5
1 5 1/10/2004 1
1 1 1/10/2005 0.5
1 0.5 1/10/2006 2
1 2 3/10/2006 0.1
1 0.1 3/12/2006 4
2 3 1/10/2001 1
2 1 1/10/2002 1
2 1 3/10/2002 2
2 2 6/10/2002 0.2
2 0.2 5/10/2003 5
if we only care about the rows
where next value >= current value
(where "slope" changes), then we
can add that criteria to the query above
(this is all predicated on my actually
understanding what you want...)
SELECT
CurrentData.ID,
CurrentData.AValue,
CurrentData.ADate,
NextData.AValue AS NextValue
FROM tblStegman AS CurrentData
INNER JOIN tblStegman AS NextData
ON CurrentData.ID = NextData.ID
WHERE
(NextData.AValue >= CurrentData.AValue)
AND
(NextData.ADate =
(SELECT Min(t.Adate)
FROM tblStegman As t
WHERE t.ID = CurrentData.ID
AND
t.ADate > CurrentData.ADate));
producing from your example data:
ID AValue ADate NextValue
1 0.5 1/10/2006 2
1 0.1 3/12/2006 4
2 1 1/10/2002 1
2 1 3/10/2002 2
2 0.2 5/10/2003 5
we can save this as "qryMins"
then final query simply picks out
the minimum date for each ID.
SELECT
qM.ID,
qM.AValue,
qM.ADate
FROM qryMins AS qM
WHERE
qM.ADate =
(SELECT MIN(q.ADate)
FROM qryMins As q
WHERE q.ID = qM.ID)
GROUP BY
qM.ID,
qM.AValue,
qM.ADate;
ID AValue ADate
1 0.5 1/10/2006
2 1 1/10/2002
I guess I'm really late now,
but this is less "dirty."
good luck,
gary