Hello. I'm a bit stuck trying to build a view in SQL Server 2008R2.I have a table that's a log of sorts, logging inputs and responses by user and by time. Something along the lines of this:CREATE TABLE Entries ( userID int NOT NULL, entryID int NOT NULL, requested_dt datetime NOT NULL, request varchar(255) NULL, response varchar(255) NULL)INSERT INTO Entries (userID, entryID, requested_dt, request, response)SELECT 12582, 154, '2011-08-01 00:01:22.000', 'catfish?', 'No. Go Fish' UNION ALLSELECT 12597, 162, '2011-08-01 00:01:53.000', 'Play a game', 'You can start' UNION ALLSELECT 12582, 164, '2011-08-01 00:02:01.000', 'ok. Grouper?', 'No. Go Fish.' UNION ALLSELECT 12597, 165, '2011-08-01 00:02:14.000', 'ok. dogfish?', 'No. Go Fish' UNION ALLSELECT 12582, 167, '2011-08-01 00:02:22.000', 'I give up.', 'You lose.' UNION ALLSELECT 12597, 170, '2011-08-01 00:03:47.000', 'clown fish.', 'Yes! You win.'
I want to generate a view that shows multiple columns for "pairs" of entries. Think of it as putting two copies of the table side by side, sorting by user and datetime, and then offsetting one of the tables by a row.I would expect the result to be something like this:UserID Request1_dt Request1 Response1 Request2 Response212582 2011-08-01 00:01:22.000 catfish? No. Go Fish. ok. Grouper? No. Go Fish12582 2011-08-01 00:02:01.000 ok. Grouper? No. Go Fish. I give up. You lose.12582 2011-08-01 00:02:22.000 I give up. You lose. NULL NULL12597 2011-08-01 00:02:33.000 Play a game You can start. ok. dogfish? No. Go Fish12597 2011-08-01 00:03:14.000 ok. dogfish? No. Go Fish. clown fish. Yes! You win.12597 2011-08-01 00:03:47.000 clown fish. Yes! You win. NULL NULL
I am able to get a single column from the "next" record for each entry via correlated subquery but I can't figure out how to grab multiple columns from the subsequent entries. I know the concept is a bit strange. Think of it as a sort of path analysis exercise. Ultimately, I'd like to have data arranged such that I can (via SQL or Excel) view the responses and/or requests that either precede or follow any given response.Clear as mud?Thanks in advance.