> You could use a subquery to return the preceding record, based
> on matching plant_ID, Control_ID, and smaller ID or entrydate.
[quoted text clipped - 6 lines]
> AND subtable.ID < table.ID) as inc_red
> FROM table;
Unless I misunderstood the post I don't think this is going to
work for a couple of reasons. The Last() function doesn't give
you the last record and without an ORDER BY clause the order
returned by the database is undefined and cannot be depending on.
Instead a solution is to look for the previous value with a
SELECT TOP 1 query (which is not portable to other DBMS systems -
it is a JET only feature).
SELECT * from table As t,
table.Tons_reduced - (SELECT TOP 1 tons_reduced
FROM table ALIAS Subtable
WHERE subtable.Control_ID = table.control_id
AND subtable.plant_ID = table.plant_ID
AND subtable.ID < t.ID
ORDER BY subtable.ID Desc) as inc_red
FROM table;
The Tons_reduced field could perhaps be substituted in the
subquery's "AND subtable.ID < t.ID" instead of ID.

Signature
'---------------
'John Mishefske
'---------------
Lyle Fairfield - 26 Feb 2006 14:07 GMT
> Instead a solution is to look for the previous value with a
> SELECT TOP 1 query (which is not portable to other DBMS systems -
> it is a JET only feature).
>From Ms-SQL Books on Line:
"You can use the TOP clause to limit the number of rows that are
returned in the result set.
TOP ( expression ) [ PERCENT ] [ WITH TIES ]
expression is a numeric expression that specifies the number of rows to
be returned; or if PERCENT is specified, the percentage (specified by
expression) of the result set rows is returned. For example:
TOP (120) /*Return the top 120 rows of the result set. */
TOP (15) PERCENT /* Return the top 15 percent of the result set. */.
TOP(@n) /* Return the top @n rows of the result set, with the variable
declaration: DECLARE @n AS BIGINT; SET @n = 2 */."
Bob Quintal - 26 Feb 2006 14:28 GMT
>> You could use a subquery to return the preceding record, based
>> on matching plant_ID, Control_ID, and smaller ID or entrydate.
[quoted text clipped - 27 lines]
> The Tons_reduced field could perhaps be substituted in the
> subquery's "AND subtable.ID < t.ID" instead of ID.
Interesting observation: you correctly picked up I had an error
in my code, but offered a different correction, then a third
variant on the theme..
I typed last() instead of the intended max()
Again we prove that there's more than one way...

Signature
Bob Quintal
PA is y I've altered my email address.