Hello,
I'm wondering if anyone might be able to help me. I am trying to group a table similar to the one below. I have seen similar problems and previously had this solved when the table had a VisitNumber column, but this has been taken away.
My Sample Data is below:
CREATE TABLE #tb
(
ClientID int NOT NULL
,BuildingID int NOT NULL
,RoomStart date NOT NULL
,RoomEnd date
,RoomStatus varchar(20)
);
INSERT INTO #tb
VALUES
(1001, 11, '20120101', '20120201', 'xfer')
,(1001, 12, '20120201', '20120301', 'xfer')
,(1001, 11, '20120301', '20120401', 'checkout')
,(1002, 11, '20120101', '20120501', 'xfer')
,(1002, 13, '20120501', '20120601', 'checkout')
,(1003, 13, '20130211', '20130218', 'checkout')
,(1003, 11, '20140601', '20140620', 'xfer')
,(1003, 13, '20140620', '20140701', 'checkout')
,(1003, 11, '20151220', NULL, NULL)
,(1004, 13, '20151220', '20151227', 'xfer')
,(1004, 11, '20151227', NULL, NULL);
What I want to do is group each visit with a start and end date (which may be null if the client is still in the room), so for example the first 2 visits would look like:
ClientID StartBuilding EndBuilding StartDate EndDate
1001 11 11 2012-01-01 2012-04-01
1002 11 13 2012-01-01 2012-06-01
Does anyone have any advice?
Thanks in advance!