使用SQL计算会话持续时间和页面停留时间 [英] Session Duration and Time on Page calculation using SQL

查看:1362
本文介绍了使用SQL计算会话持续时间和页面停留时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Netezza的web_event表中有以下这种格式的数据.

I have some data in web_event table of Netezza in this below format.

vstr_id  |  sessn_id  |  sessn_ts            | wbpg_nm 
V1       |  V1S1      |  02-02-2015 09:20:00 | /home/login
V1       |  V1S1      |  02-02-2015 09:30:00 | /home/contacts
V1       |  V1S1      |  02-02-2015 09:50:00 | /home/search
V2       |  V2S1      |  02-02-2015 09:10:00 | /home
V2       |  V2S1      |  02-02-2015 09:15:00 | /home/apps
V2       |  V2S2      |  02-02-2015 09:20:00 | /home/news
V2       |  V2S2      |  02-02-2015 09:23:00 | /home/news/internal

这是我的源表.

我正在尝试使用该web_event表并创建另一个如下表.

I am trying to use that web_event table and create another table like below.

我希望像下面那样加载sessn_durtn表和time_on_pg表.

I want the sessn_durtn table and time_on_pg table to be loaded like below.

1)sessn_durtn列:根据已排序的时间字段,这应该是会话开始事件和会话结束事件之间的时间差.可以用分钟或秒来表示

1) sessn_durtn column : this should be time difference between session start event and session end event according to sorted time field. It can be in represented in minutes or seconds

I am trying to to do 

Insert into sessn_durtn (select VSTR_ID,
           SESSN_ID,
           ????? as sessn_durtn,
           from web_event)

vstr_id  |  sessn_id  | seesn_durtn
V1       |  V1S1      | 30mins       
V2       |  V2S1      | 5mins                
V2       |  V2S2      | 3mins          

2)time_on_page列:当前页面与下一页和会话的最后一页之间的时间差可以为0秒.它可以用分钟或秒表示.

2) time_on_page column : It is the time difference between the current page and next page and the last page of the session can have 0 secs. It can be represented in minutes or seconds.

Insert into time_on_pg (select VSTR_ID,
           SESSN_ID,
           sessn_ts,
           WBPG_NM,
           ????? as time_on_page
           from web_event)

vstr_id  |  sessn_id  |  sessn_ts            | wbpg_nm              | time_on_page
V1       |  V1S1      |  02-02-2015 09:20:00 | /home/login          |   10mins
V1       |  V1S1      |  02-02-2015 09:30:00 | /home/contacts       |   20mins
V1       |  V1S1      |  02-02-2015 09:50:00 | /home/search         |   0mins
V2       |  V2S1      |  02-02-2015 09:10:00 | /home                |   5mins
V2       |  V2S1      |  02-02-2015 09:15:00 | /home/apps           |   0mins
V2       |  V2S2      |  02-02-2015 09:20:00 | /home/news           |   3mins
V2       |  V2S2      |  02-02-2015 09:23:00 | /home/news/internal  |   0mins

我们如何在Netezza或任何SQL查询中做到这一点?

推荐答案

会话期间:

SELECT vstr_id, MAX(sessn_ts), MIN(sessn_ts), 
TIMESTAMPDIFF(MINUTE,MIN(sessn_ts),MAX(sessn_ts)) AS sessn_durtn
FROM `web_event`
GROUP BY vstr_id, sessn_id

对于页面停留时间(您没有记录他们离开的时间,因此我无法获取会话最后一页的页面停留时间,因此我将其设置为0.如果您拥有该数据,则可以使用固定的wbpg_nm插入该数据,该wbpg_nm不会与其他任何碰撞,例如退出"或类似的东西):

And for the time on page (you don't have a record of the time they leave, so I can't get the time on page for the last page of the session, so I just set that to 0. If you have that data, you could insert it with a fixed wbpg_nm that won't collide with any others, maybe 'exit' or some such):

SELECT t1.*,
IFNULL(TIMESTAMPDIFF(MINUTE, t1.sessn_ts, t2.sessn_ts), 0) AS time_on_pg
FROM
(
    SELECT w1.*,
    @rownum := @rownum + 1 AS position
    FROM `web_event` w1
    JOIN (SELECT @rownum := 0) r
    ORDER BY vstr_id, sessn_id, sessn_ts
) t1
LEFT JOIN
(
    SELECT w1.*,
    @rownum2 := @rownum2 + 1 AS position
    FROM `web_event` w1
    JOIN (SELECT @rownum2 := 0) r
    ORDER BY vstr_id, sessn_id, sessn_ts
) t2
ON t1.vstr_id = t2.vstr_id 
AND t1.sessn_id = t2.sessn_id 
AND t1.position = t2.position - 1

这篇关于使用SQL计算会话持续时间和页面停留时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆