Im using SQL Server 2005. From the tbl_temp table below, I would like to add an EndDate column based on the next row's StartDate minus 1 day until there's a change in AID and UID combination. This calculated EndDate will go to the row above it as the EndDate. The last row of the group of AID and UID will get the system date as its EndDate. The table has to be ordered by AID, UID, StartDate sequence. Thanks for the help.
-- tbl_temp
AID UID StartDate 1 1 2013-02-20 2 1 2013-02-06 1 1 2013-02-21 1 1 2013-02-27 1 2 2013-02-02 1 2 2013-02-04
-- Result needed
AID UID StartDate EndDate 1 1 2013-02-20 2013-02-20 1 1 2013-02-21 2013-02-26 1 1 2013-02-27 sysdate 1 2 2013-02-02 2013-02-03 1 2 2013-02-04 sysdate 2 1 2013-02-06 sysdate