I have a table with many rows of data, which has an column filled with a date.
I need to select their first row and their last row between two dates.
Example:
Score table:
id userId value1 value2 date
1 1 1 2 2012-09-01 00:00:00
2 1 5 8 2012-09-03 00:00:00
3 1 7 11 2012-09-04 00:00:00
4 1 9 14 2012-09-05 00:00:00
I need to get the values value1 and value2 between the dates 2012-09-02 00:00:00 and 2012-09-04 00:00:00 of userId 1.
The desired result should be:
userId value1_old value2_old value1_new value2_new
1 5 8 7 11
The values _old should contain the data older then, but closest to the date 2012-09-02 00:00:00.
The values _new should contain the data earlier then, but closest to the date 2012-09-04 00:00:00
The problem is, how do I get these data? I don’t know how to get this to work.
Thanks in advance,
Mark
See it on sqlfiddle.