3

抱歉,如果我的标题没有正确描述我要执行的任务。

对于一个大学项目,我收到了一个网站的访问日志,我把不需要的栏目去掉了,浓缩成这样:

╔══════════╦══════════════════════╦═════════════════╦═════════════╦════════════════╗
║ accessid ║ date_time_in_seconds ║ yg_requester_id ║ referent_id ║ referent_docid ║
╠══════════╬══════════════════════╬═════════════════╬═════════════╬════════════════╣
║     2449 ║        2009011621830 ║           32276 ║       12648 ║              1 ║
║     2776 ║        2009011622726 ║           76360 ║       11070 ║              1 ║
║     2804 ║        2009011622783 ║           32276 ║       13845 ║              1 ║
║     2894 ║        2009011623025 ║           32276 ║        7222 ║              1 ║
║     2895 ║        2009011623037 ║           32276 ║        1530 ║              1 ║
║     3000 ║        2009011623406 ║           32276 ║        3728 ║              1 ║
║     3019 ║        2009011623497 ║          520060 ║       10356 ║              1 ║
║     3245 ║        2009011625780 ║          300841 ║        4607 ║              1 ║
║     3274 ║        2009011628309 ║          532664 ║       14377 ║              1 ║
║     3275 ║        2009011628420 ║          532664 ║        9097 ║              1 ║
╚══════════╩══════════════════════╩═════════════════╩═════════════╩════════════════╝

最初,时间和日期戳在每个测量单位(年、月、日、小时、分钟、秒)的单独列中,为了便于计算,我将它们合并为具有格式的 date_time_in_seconds

[0000][00][00][00000]
[YEAR][MONTH][DAY][Number of Seconds since 00:00]

accessid 是表条目 ID,yg_requester_id 是网站访问者的唯一 ID,referent_id 是他们阅读的网站文章的 ID,referent_docid 表示文章的类型,但在此任务中不需要。

基本上,我希望能够找到自最后一个不同的 referent_id 被同一个 yg_requester_id 访问以来的时间差。例如,查看上表中的这一部分行:

╔══════════╦══════════════════════╦═════════════════╦═════════════╦════════════════╗
║ accessid ║ date_time_in_seconds ║ yg_requester_id ║ referent_id ║ referent_docid ║
╠══════════╬══════════════════════╬═════════════════╬═════════════╬════════════════╣
║     2449 ║        2009011621830 ║           32276 ║       12648 ║              1 ║
║     2776 ║        2009011622726 ║           76360 ║       11070 ║              1 ║
║     2804 ║        2009011622783 ║           32276 ║       13845 ║              1 ║
╚══════════╩══════════════════════╩═════════════════╩═════════════╩════════════════╝

yg_requester_id 32276在2009 年 1 月 16 日06:03:50(午夜后21830秒)访问了 id为12648的文章。然后他们在 16 日06:19:43(午夜后22783秒)访问了 id为13845的文章2009 年 1 月。因此可以安全地假设用户阅读了第一篇文章(id 12648)大约 15 分 50 秒

我想找到的是同一用户访问的文章之间的时间差。用户连续阅读的文章可能没有连续的 accessid(尽管它总是会增加)。我还想将读取时间限制为大约一个小时,因为任务是过滤掉读取时间低于可变分钟数(例如 15 分钟)的记录。

提前致谢,如果需要更多信息,请告诉我

4

2 回答 2

2

我将使用 ROW_NUMBER 按 yg_requester_id 对结果集进行分区,并按 accessid 或 datetime 对其进行排序(假设您要将 date_time_in_seconds 列更改为常规的 datetime 列,如评论中所建议的那样。然后我将由请求者和到以前的记录,并得到差异。

让我尝试在没有正确数据的情况下编写查询:

SELECT X1.yg_requester_id, DATEDIFF(SECOND, X1.NewDateTimeField, X2.NewDateTimeField) AS TimeDifferenceInSeconds, X1.referent_id AS NewArticle, X2.referent_id AS FormerArticle
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY yg_requester_id ORDER BY NewDateTimeField DESC) AS Position, NewDateTimeField, yg_requester_id, referent_id
FROM YourTable

) X1
INNER JOIN 
(
SELECT ROW_NUMBER() OVER(PARTITION BY yg_requester_id ORDER BY NewDateTimeField DESC) AS Position, NewDateTimeField, yg_requester_id, referent_id
FROM YourTable  
) X2 ON X2.yg_requester_id = X1.yg_requester_id AND X2.Position = X1.Position - 1
于 2012-10-02T10:40:01.040 回答
0

此查询应检索请求者、所指对象以及请求者在所指对象上所采用的时间差(以秒为单位):

select abc.A_requestor as requestor_id,abc.B_refer as referent_id,abc.A_datetime-abc.B_datetime as time_difference   from 
(select a.accessid as A_accessid ,b.accessid as B_accessid,
a.yg_requestor_id as A_requestor,a.date_time_in_seconds as A_datetime,a.referent_id as A_refer,
b.yg_requestor_id as B_requestor,b.date_time_in_seconds as B_datetime,b.referent_id as B_refer
from weblog a
inner join weblog b
on a.yg_requestor_id = b.yg_requestor_id
and a.date_time_in_seconds > b.date_time_in_seconds
and a.referent_id != b.referent_id) abc

inner join 

(select cte.B_accessid,min(cte.A_accessid) as C_accessid from
(select a.accessid as A_accessid ,b.accessid as B_accessid,
a.yg_requestor_id as A_requestor,a.date_time_in_seconds as A_datetime,a.referent_id as A_refer,
b.yg_requestor_id as B_requestor,b.date_time_in_seconds as B_datetime,b.referent_id as B_refer
from weblog a
inner join weblog b
on a.yg_requestor_id = b.yg_requestor_id
and a.date_time_in_seconds > b.date_time_in_seconds
and a.referent_id != b.referent_id) cte 
group by cte.B_accessid ) xyz

on xyz.B_accessid = abc.B_accessid and xyz.C_accessid = abc.A_accessid
于 2012-10-02T11:03:18.593 回答