In MSSQL 2008 I have table and data which looks like this
create table #tempData
(user_id int,type varchar(10),ts datetime)
insert into #tempData
select 1,'ENTER','2011-01-30 15:00:00'
union all
select 1,'EXIT','2011-01-31 16:00:00'
union all
select 1,'ENTER','2011-02-1 18:00:00'
union all
select 1,'EXIT','2011-02-10 21:00:00'
union all
select 2,'ENTER','2011-01-10 21:00:00'
union all
select 2,'EXIT','2011-01-12 21:00:00'
union all
select 2,'ENTER','2011-01-13 01:00:00'
union all
select 2,'EXIT','2011-01-13 18:00:00'
--AND SO ON --
Now I trying to make query which is going to tell how long one user was inside according to last exit
something like
user_id,exited,time_in_hours
1,'2011-01-31 16:00:00',25
1,'2011-02-10 21:00:00',219
2,'2011-01-12 21:00:00',48
2,'2011-01-13 18:00:00',17
Sorry if this BAD or ROUGE question and if this is not proper way to ask a question like this.
But I am stacked on this for while.
Assuming that
same-user sessions do not intersect,
there cannot be an EXIT record without the corresponding ENTER record,
you could try the following: