Given
create table rating (
ID int identity(1,1) primary key,
PersonID int,
Ratingdate date,
rating varchar(2)
);
insert into rating values ( 1, '2010-08-04' , 'A3');
insert into rating values ( 1, '2010-08-14' , 'A1');
insert into rating values ( 2, '2010-08-04' , 'G2');
insert into rating values ( 2, '2010-08-14' , 'G1');
insert into rating values ( 3, '2010-08-04' , 'G1');
insert into rating values ( 3, '2010-08-10' , 'G1');
insert into rating values ( 3, '2010-08-14' , 'G3');
insert into rating values ( 2, '2010-08-20' , 'G4');
I want a query, which returns all rows, where the rating changes for a given person. For example here I want to exclude the row ( 3, '2010-08-10' , 'G1') because the rating didn't change since ( 3, '2010-08-04' , 'G1').
I know a solution using a cursor, but I wonder, if it could be done without cursors. I tried a bit with analytical functions, but didn't found a solution.
Besides the solution, please improve the terms with which I describe the problem. I guess it is some standard task.
Edit:
Here is the link to connect asking to add the lag and lead functions to SQL Server.
Please vote.