I have spent all morning on this and just can't get it right... I'd really appreciate the help of someone more knowledgable than myself to get this working.
I have a table with some data in that looks like this:
MonthYear WeekBeg. Week Value
Dec-10 27/12/2010 1 66.66
Jan-11 3/01/2011 2 50
Jan-11 10/01/2011 3 17.5
Jan-11 17/01/2011 4 20
Jan-11 24/01/2011 5 0
Jan-11 31/01/2011 6 50
Feb-11 7/02/2011 7 0
Feb-11 14/02/2011 8 74
Feb-11 21/02/2011 9 100
I'm sorry the table above doesn't look better... I need to calculate the difference between the values from week to week - so the results column in this case would be:
16.66
32.5
2.5
20
50
50
74
26
I've looked at lots of code on the net - (e.g. from this site) but can't seem to make it work. I added in the ABS function to make sure the differences were absolute values and got this working but the numbers themselves just aren't right.
I haven't posted what I ended up with as it just got into a bigger and bigger mess, but what I started with was the link above. Again, I'd be really grateful for any insight anyone is able to offer.
Many thanks
ADDED:
Thanks so much for the fast reply. Got this working easily - added a few bits:
SELECT T1.MonthYear AS [From], T2.MonthYear AS [To], T1.Week AS Week, T1.WeekBeg AS WeekBeg, ABS(T1.Value - T2.Value) AS Difference FROM Test AS T1 LEFT JOIN Test AS T2 ON T2.Week = T1.Week + 1
Only thing is the resulting difference values need to be in the second of the two rows whereas here they are in the first of the two. Is there any easy way of modifying this?
Many thanks again.
ADDED:
Would definitely be worth using the second option if possible as can't always guarantee weeks won't be missed out. I am probably missing something, but when I run the second option from Thomas, I get the message:
'The specified field [T1].[Datavalue] could refer to more than one table listed in the FROM clause of your SQL statement'.
I thought this might be to do with the field in the table being VALUE not DataValue, but when I change it, I get 'Type Mismatch in Expression' instead.
Many thanks.