Dear Jay:
OK, it looks like it is starting to be possible.
I'm assuming here that this is sorted by UnitName and MW, and that,
together, these columns are unique.
The trick is to be able to find the previous row, when there is one. The
previous row I will define as being a row:
- with the same UnitName
- with the largest value of MW less than the current row
I code that as follows:
SELECT UnitName, MW, Cost,
MW - NZ(SELECT MAX(MW) FROM Basic_Calcs BC1
WHERE BC1.UnitName = BC.UnitName
AND BC1.MW < BC.MW), MW) AS WhatIWant
FROM Basic_Calcs BC
ORDER BY UnitName, MW
The above will give you zero for the first row of each UnitName. Your
sample had this column blank in one instance and Null in another. I'm not
sure what you really want in this instance. I would have thought the
WhatIWant column would have the whole Cost in it when it is the first row,
that is, 1000 in the first one, and 4000 in the second, as the previous row
(which doesn't exist) is effectively zero. That way, a running sum of the
WhatIWant column would be equal to the current Cost value. Makes sense to
me. For this, use:
SELECT UnitName, MW, Cost,
MW - NZ(SELECT MAX(MW) FROM Basic_Calcs BC1
WHERE BC1.UnitName = BC.UnitName
AND BC1.MW < BC.MW), 0) AS WhatIWant
FROM Basic_Calcs BC
ORDER BY UnitName, MW
Does this start to give you what you want?
The technique is known as a correlated subquery, and you must also use
Aliasing. These topics may give you something to search and learn about how
this is done.
Tom Ellison