I think you're going to need more than just a simple query to do this. Access (or more correctly, SQL) has a bit of a problem doing record-by-record manipulations like this, where essentially you need to look at the "previous" record within this "key" in order to make a calculation. This LOGICAL KEY operation means that a simple "PREVIOUS" positional operation will not necessarily be correct. So essentially you need to
1) Grab the First record.
2) Set this "Previous Amount" field to "null"
3) Save the "current" amount value.
4) Go to the NEXT LOGICAL Record according to KEY structure.
5) Compare the Amount HERE to the amount "saved" in step3 to do your calculation
6) Go to step 3 and repeat as necessary.
See the problem? SQL is a SET oriented guy, not a record-by-record thing.
This is NOT to say that this can not be done. You have two options - restructure the table a bit, so that you STORE the value from the prior year when you add a new record for an employee for a year. While not relationally pure, and Celko and Date would probably skin me alive for it, will work so long as maintenance to these records is controlled and the possibility of fudging with the records is minimized.
The other option is to write a function that will populate a temporary table with your values, using recordset movement operations, and then perhaps using a query or two on it. Since I hate writing code when a database operation will suffice, I would probably lean to the former option.
Perhaps some other denizens here can offer you a better answer with a SQL based solution that I do not have the expertise to come up with right now.
I'd be happy to persue this if you wish, though, because it's an interesting, and common enough, problem.
Jim
Me? Ambivalent? Well, yes and no....
Another free Access forum:
More Access stuff at