I have a table that displays the changes in Conditions (condition_id) of Rooms (room_id) and when did it happened (date_registered). SQL Server 2008-R2.
I want to build a query that for a given date period displays the changes in room conditions of every room that changed next to its previous condition
Here is an example
DECLARE @tbl TABLE (
room_id numeric(10,0) null,
condition_id numeric(10,0) null,
date_registered datetime null
);
INSERT @tbl (room_id, condition_id, date_registered)
VALUES
(1,2,'2018-12-07 08:37:19.300'),
(2,1,'2018-12-08 08:37:19.300'),
(1,3,'2018-12-09 08:37:19.300'),
(2,2,'2018-12-10 08:37:19.300')
(1,1,'2018-12-11 08:37:19.300');
I would like to end up with a result like that:
room_id old_condition_id condition_id date_registered
1 2 3 2018-12-09 08:37:19.300
2 1 2 2018-12-10 08:37:19.300
1 3 1 2018-12-11 08:37:19.300
Thank you