27,579
社区成员
发帖
与我相关
我的任务
分享
------------------------------------
-- Author:Flystone
-- Version:V1.001
-- Date:2008-08-07 23:15:54
------------------------------------
-- Test Data: OP_Act
If object_id('OP_Act') is not null
Drop table OP_Act
Go
Create table OP_Act(userid int,act nvarchar(2),time_stamp datetime)
Go
Insert into OP_Act
select 1001,'浏览','2008-6-1 11:58:00.000' union all
select 1001,'详细','2008-6-1 12:01:00.000' union all
select 1001,'订购','2008-6-1 12:02:00.000' union all
select 1001,'退出','2008-6-1 12:05:00.000' union all
select 1001,'登陆','2008-6-1 14:00:00.000'
Go
--Start
select * into # from op_act
union all
Select userid,act,dateadd(mi,60 - datepart(mi,time_stamp), time_stamp) from OP_Act a
where not exists(select 1 from op_act where datepart(hh,time_stamp) = datepart(hh,a.time_stamp)
and time_stamp > a.time_stamp)
order by time_stamp
select userid,act,time_stamp,
timelong = datediff(ss,time_stamp,isnull((select top 1 time_stamp from # where time_stamp > a.time_stamp),time_stamp))
from # a
drop table #
--Result:
/*
userid act time_stamp timelong
----------- ---- ------------------------------------------------------ -----------
1001 浏览 2008-06-01 11:58:00.000 120
1001 浏览 2008-06-01 12:00:00.000 60
1001 详细 2008-06-01 12:01:00.000 60
1001 订购 2008-06-01 12:02:00.000 180
1001 退出 2008-06-01 12:05:00.000 3300
1001 退出 2008-06-01 13:00:00.000 3600
1001 登陆 2008-06-01 14:00:00.000 3600
1001 登陆 2008-06-01 15:00:00.000 0
(所影响的行数为 8 行)
*/
--End
------------------------------------
-- Author:Flystone
-- Version:V1.001
-- Date:2008-08-07 23:15:54
------------------------------------
-- Test Data: OP_Act
If object_id('OP_Act') is not null
Drop table OP_Act
Go
Create table OP_Act(userid int,act nvarchar(2),time_stamp datetime)
Go
Insert into OP_Act
select 1001,'浏览','2008-6-1 11:58:00.000' union all
select 1001,'详细','2008-6-1 12:01:00.000' union all
select 1001,'订购','2008-6-1 12:02:00.000' union all
select 1001,'退出','2008-6-1 12:05:00.000' union all
select 1001,'登陆','2008-6-1 14:00:00.000'
Go
--Start
select * from op_act
union all
Select userid,act,dateadd(mi,60 - datepart(mi,time_stamp), time_stamp) from OP_Act a
where not exists(select 1 from op_act where datepart(hh,time_stamp) = datepart(hh,a.time_stamp)
and time_stamp > a.time_stamp)
order by time_stamp
--Result:
/*
userid act time_stamp
----------- ---- ------------------------------------------------------
1001 浏览 2008-06-01 11:58:00.000
1001 浏览 2008-06-01 12:00:00.000
1001 详细 2008-06-01 12:01:00.000
1001 订购 2008-06-01 12:02:00.000
1001 退出 2008-06-01 12:05:00.000
1001 退出 2008-06-01 13:00:00.000
1001 登陆 2008-06-01 14:00:00.000
1001 登陆 2008-06-01 15:00:00.000
(所影响的行数为 8 行)
*/
--End