I’m not sure if this is possible in T-SQL alone or if I should move this into c# and do it there. I have a table setup like so:
Tracking
--------
ID int
Hash varchar
pageID int
The table is tracking how a user travels though a website. Every page load a new line is added. Each session gets a new hash. Example data:
1 3pm6Qav1Vdf 4
2 3pm6Qav1Vdf 7
3 3pm6Qav1Vdf 41
4 3pm6Qav1Vdf 2
So doing the following will get the trail (first page to last page) for a session:
SELECT * from Tracking WHERE hash='3pm6Qav1Vdf' ORDER BY ID;
What I would like to get if possible would be:
1 3pm6Qav1Vdf NULL 4
2 3pm6Qav1Vdf 4 7
3 3pm6Qav1Vdf 7 41
4 3pm6Qav1Vdf 41 2
It would add a column listing the previous pageID as well as the current pageID. Is something like this possible in T-SQL or should I move it to c#?
This CTE will give you access to the previous row. You could move the WHERE clause to the CTE portion if you’d like, or you could leave it out entirely to get your entire rowset with previous values.