34,838
社区成员




select
A.F_loginid,
A.F_logintime,
(select
min(F_logintime)
from A B
where B.F_Event=0 and B.F_loginid =A.F_loginid
and B.F_logintime>A.F_logintime)
AS F_LogoutTime,
F_name ,
F_event
from A
where f_event =1
order by F_loginid
declare @t table(F_loginid varchar(10),F_logintime datetime,F_name varchar(10),F_Event int)
insert into @t select '0001','2008-04-03 14:40:04','aaa',1
insert into @t select '0001','2008-04-03 14:40:59','aaa',0
insert into @t select '0002','2008-04-03 14:42:09','aaa',1
insert into @t select '0002','2008-04-03 14:44:13','aaa',0
select a.f_name,a.F_logintime,b.F_logintime,
F_ontimes=convert(varchar(10),b.F_logintime-a.F_logintime,108)
from @t a,@t b
where a.f_name=b.f_name and a.F_loginid=b.F_loginid and b.f_event=0 and a.f_event=1
select f_loginid,
f_name,
max(case when f_event = 1 then f_logintime else null end) as f_logintime,
max(case when f_event = 0 then f_logintime else null end) as f_logintime,
max(case when f_event = 1 then f_logintime else null end) as f_logintime -
max(case when f_event = 0 then f_logintime else null end) as f_logintime,
from ta
group by f_loginid,f_name