34,591
社区成员
发帖
与我相关
我的任务
分享
set nocount on
go
/* 关于log记录配对 * log 通常包括portid / sessionid, 该id通常会被重用,但同一时刻不会重复 * 一组记录通常包含两条:先入,后出 */
declare @logdata table (
logid int not null, -- 类似 portid / sessionid, 该id通常会被重用,但同一时刻系统中不会重复出现
logtype tinyint, -- 方向, 1: login|入, 0: logout|出
logtime datetime -- 时间点
)
insert @logdata
select 1, 1 ,'2008-4-3 11:23:41' union
select 1, 0 ,'2008-4-3 15:23:41' union
select 1, 0 ,'2008-4-4 10:23:47' union -- 应该抛弃的记录
select 1, 1 ,'2008-4-5 10:22:47' union -- 应该抛弃的记录
select 1, 1 ,'2008-4-5 10:25:47' union
select 1, 0 ,'2008-4-7 14:23:11'
select a.logid, logon=a.logtime, logoff=b.logtime, minutes=ceiling(datediff(second,a.logtime,b.logtime)/60.0)
from @logdata a
join @logdata b on a.logid=b.logid and a.logtype!=b.logtype and b.logtime>a.logtime
and not exists (select 1 from @logdata b0 where b0.logid=b.logid and b0.logtype=b.logtype
and b0.logtime>a.logtime and b0.logtime<b.logtime)
where a.logtype=1 -- logtype=1为开始标记
and not exists (select 1 from @logdata a0
where a0.logid=a.logid and a0.logtype=a.logtype and a0.logtime>a.logtime
and a0.logtime < b.logtime)
go
set nocount on
go
/* 关于log记录配对 * log 通常包括portid / sessionid, 该id通常会被重用,但同一时刻不会重复 * 一组记录通常包含两条:先入,后出 */
declare @logdata table (
logid char(36) not null, -- 类似 portid / sessionid, 该id通常会被重用,但同一时刻系统中不会重复出现
logtype tinyint, -- 方向, 1: login|入, 0: logout|出
logtime datetime -- 时间点
)
insert @logdata
select 1, 1 ,'2008-4-3 11:23:41' union
select 1, 0 ,'2008-4-3 15:23:41' union
select 1, 0 ,'2008-4-4 10:23:47' union
select 1, 1 ,'2008-4-5 10:25:47' union
select 1, 0 ,'2008-4-7 14:23:11'
declare @logI table (id int identity(1,1), logid char(36), logItime datetime)
insert into @logI -- 各个有效的进入时间点
select logid, logItime=logtime from @logdata a
where logtype = 1
and exists (
-- 本次退出的时间
select 1 from @logdata c where c.logid=a.logid and c.logtype=0 and c.logtime>a.logtime )
and not exists ( select 1 where
-- 本次退出的时间
( select min(logtime) from @logdata c where c.logid=a.logid and c.logtype=0 and c.logtime>a.logtime )
>
-- 下次进入的时间
( select min(logtime) from @logdata b where logid=a.logid and logtype=1 and b.logtime>a.logtime )
)
order by logid, logItime
declare @logO table (id int identity(1,1), logid char(36), logOtime datetime)
insert into @logO -- 各个有效的退出时间点
select logid, logOtime=logtime from @logdata a
where logtype = 0
and exists (
-- 本次进入的时间
select 1 from @logdata c where c.logid=a.logid and c.logtype=1 and c.logtime<a.logtime )
and not exists ( select 1 where
-- 本次进入的时间
( select max(logtime) from @logdata c where c.logid=a.logid and c.logtype=1 and c.logtime<a.logtime )
<
-- 上次退出的时间
( select max(logtime) from @logdata b where logid=a.logid and logtype=0 and b.logtime<a.logtime )
)
order by logid, logOtime
-- 配对:
select a.logid, loginTime = logItime, logoutTime = logOtime from @logI a, @logO b where a.id=b.id
--
-- logid,loginTime,logoutTime
-- 1,2008-04-03 11:23:41,2008-04-03 15:23:41
-- 1,2008-04-05 10:25:47,2008-04-07 14:23:11
--
declare @t table(kh int,jqh int,sj datetime)
insert @t select
1 , 1 ,'2008-4-3 11:23:41' union select
1 , 2 ,'2008-4-3 15:23:41' union select
1 , 2 ,'2008-4-4 10:23:47' union select
1 , 1 ,'2008-4-5 10:25:47' union select
1 , 2 ,'2008-4-7 14:23:11'
select px=identity(int,1,1), * into # from @t order by sj
select isnull(a.kh,b.kh) as kh,isnull(a.sj,'1900-01-01') sj1,isnull(b.sj,'1900-01-01') sj2,
[sj1-sj2 ]= datediff(mi,isnull(a.sj,dateadd(mi,1,b.sj)),isnull(b.sj,dateadd(mi,-1,a.sj)))
from
(select * from # where jqh = 1) a full join
(select * from # where jqh = 2) b
on a.kh = b.kh and a.px = b.px - 1
drop table #
/*
kh sj1 sj2 sj1-sj2
----------- ------------------------------------------------------ ------------------------------------------------------ -----------
1 2008-04-03 11:23:41.000 2008-04-03 15:23:41.000 240
1 1900-01-01 00:00:00.000 2008-04-04 10:23:47.000 -1
1 2008-04-05 10:25:47.000 2008-04-07 14:23:11.000 3118
(所影响的行数为 3 行)
*/
declare @t table(kh int,jqh int,sj datetime)
insert @t select
1 , 1 ,'2008-4-3 11:23:41' union select
1 , 2 ,'2008-4-3 15:23:41' union select
1 , 2 ,'2008-4-4 10:23:47' union select
1 , 1 ,'2008-4-5 10:25:47' union select
1 , 2 ,'2008-4-7 14:23:11'
select px=identity(int,1,1), * into # from @t order by sj
select isnull(a.kh,b.kh) as kh,isnull(a.sj,'1900-01-01') sj1,isnull(b.sj,'1900-01-01') sj2,
[sj1-sj2 ]= datediff(mi,isnull(a.sj,dateadd(mi,1,b.sj)),isnull(b.sj,dateadd(mi,-1,a.sj)))
from
(select * from # where jqh = 1) a full join
(select * from # where jqh = 2) b
on a.kh = b.kh and a.px = b.px - 1
drop table #
/*
kh sj1 sj2 sj1-sj2
----------- ------------------------------------------------------ ------------------------------------------------------ -----------
1 2008-04-03 11:23:41.000 2008-04-03 15:23:41.000 240
1 2008-04-04 10:23:47.000 2008-04-04 10:23:47.000 0
1 2008-04-05 10:25:47.000 2008-04-07 14:23:11.000 3118
(所影响的行数为 3 行)
*/
declare @t table(kh int,jqh int,sj datetime)
insert @t select
1 , 1 ,'2008-4-3 11:23:41' union select
1 , 2 ,'2008-4-3 15:23:41' union select
1 , 2 ,'2008-4-4 10:23:47' union select
1 , 1 ,'2008-4-5 10:25:47' union select
1 , 2 ,'2008-4-7 14:23:11'
select px=identity(int,1,1), * into # from @t order by sj
select isnull(a.kh,b.kh) as kh,isnull(a.sj,b.sj) sj1,isnull(b.sj,'1900-01-01') sj2,
[sj1-sj2 ]= datediff(mi,isnull(a.sj,b.sj),isnull(b.sj,b.sj))
from
(select * from # where jqh = 1) a full join
(select * from # where jqh = 2) b
on a.kh = b.kh and a.px = b.px - 1
drop table #
/*
kh sj1 sj2 sj1-sj2
----------- ------------------------------------------------------ ------------------------------------------------------ -----------
1 2008-04-03 11:23:41.000 2008-04-03 15:23:41.000 240
1 2008-04-04 10:23:47.000 2008-04-04 10:23:47.000 0
1 2008-04-05 10:25:47.000 2008-04-07 14:23:11.000 3118
(所影响的行数为 3 行)
*/
select datediff(day, '2007-12-12','2008-12-12')
不明白你的意思