I'm really struggling with something that should be straight forward, I have a table in the following format:
ID Date Agent State
1 02/01/2015 77777 Work
2 03/01/2015 77777 X
3 04/01/2015 77777 X
4 05/01/2015 77777 X
5 06/01/2015 77777 X
6 07/01/2015 77777 SICK
7 08/01/2015 77777 SICK
8 09/01/2015 77777 X
9 02/01/2015 88888 Work
10 03/01/2015 88888 Work
11 04/01/2015 88888 SICK
12 05/01/2015 88888 SICK
13 06/01/2015 88888 X
14 07/01/2015 88888 X
15 08/01/2015 88888 SICK
16 09/01/2015 88888 Work
I need to create a loop that replaces the X's with the state of the preceding record until it reaches a field that doesn't contain X. It would also have to cater for a change in Agent ID and also if the first record for an agent is X then it should show "Work"
The expected output would look like:
Date Agent State
02/01/2015 77777 Work
03/01/2015 77777 Work
04/01/2015 77777 Work
05/01/2015 77777 Work
06/01/2015 77777 Work
07/01/2015 77777 SICK
08/01/2015 77777 SICK
09/01/2015 77777 SICK
02/01/2015 88888 Work
03/01/2015 88888 Work
04/01/2015 88888 SICK
05/01/2015 88888 SICK
06/01/2015 88888 SICK
07/01/2015 88888 SICK
08/01/2015 88888 SICK
09/01/2015 88888 Work
I'm using sql server management studio 2008