用户行为统计分析页面停留时间计算

select * from T_LOG_DETAIL_WEB_TEMP_TIME;

--用户登录,userid不为空
select * from T_LOG_DETAIL_WEB_TEMP_TIME where userid is not null order by userid,accesstime;

--给RN赋值
select userid,accesstime,rank() over(partition by userid order by accesstime) rn from T_LOG_DETAIL_WEB_TEMP_TIME where userid is not null;

begin
  for t2 in (select userid,accesstime,rank() over(partition by userid order by accesstime) rn from T_LOG_DETAIL_WEB_TEMP_TIME where userid is not null) loop
    update T_LOG_DETAIL_WEB_TEMP_TIME t
    set t.rn=t2.rn
    where t.userid=t2.userid
    and t.accesstime=t2.accesstime
    and t.userid is not null;
    commit;
  end loop;
end;

--根据RN给浏览时间赋值
select userid,accesstime+0 accesstime,rn from T_LOG_DETAIL_WEB_TEMP_TIME where userid is not null;

begin
  for t2 in (select userid,accesstime+0 accesstime,rn from T_LOG_DETAIL_WEB_TEMP_TIME where userid is not null) loop
    update T_LOG_DETAIL_WEB_TEMP_TIME t
    set t.browsetime=case when ((t2.accesstime-(t.accesstime+0))* 60 * 60 * 24) > 1800 then 1800
    when ((t2.accesstime-(t.accesstime+0))* 60 * 60 * 24) <= 0 then 1
      else    
    round(((t2.accesstime-(t.accesstime+0))* 60 * 60 * 24),2)
    end
    where t.userid is not null
    and t.userid=t2.userid
    and t.rn=t2.rn-1;
    commit;
  end loop;
end;

--给获取不到最后连接的停留时间赋值 1800
update T_LOG_DETAIL_WEB_TEMP_TIME t set t.browsetime=1800 where t.userid is not null and t.browsetime is null;
commit;

--用户未登录,userid为空
select * from T_LOG_DETAIL_WEB_TEMP_TIME where userid is null order by ip,proxy_ip,accesstime;

--给RN赋值
select ip,proxy_ip,accesstime,rank() over(partition by ip,proxy_ip order by accesstime) rn from T_LOG_DETAIL_WEB_TEMP_TIME where userid is null;

begin
  for t2 in (select ip,proxy_ip,accesstime accesstime ,rank() over(partition by ip,proxy_ip order by accesstime) rn from T_LOG_DETAIL_WEB_TEMP_TIME where userid is null) loop
    update T_LOG_DETAIL_WEB_TEMP_TIME t
    set t.rn=t2.rn
    where t.ip||t.proxy_ip=t2.ip||t2.proxy_ip
    and t.accesstime=t2.accesstime
    and t.userid is null;
    commit;
  end loop;
end;

--根据RN给浏览时间赋值
select ip,proxy_ip,accesstime+0 accesstime,rn from T_LOG_DETAIL_WEB_TEMP_TIME where userid is null;

begin
  for t2 in (select ip,proxy_ip,accesstime accesstime,rn from T_LOG_DETAIL_WEB_TEMP_TIME where userid is null) loop
    update T_LOG_DETAIL_WEB_TEMP_TIME t
    set t.browsetime=case when ((t2.accesstime-(t.accesstime+0))* 60 * 60 * 24) > 1800 then 1800
    when ((t2.accesstime-(t.accesstime+0))* 60 * 60 * 24) <= 0 then 1
      else    
    round(((t2.accesstime-(t.accesstime+0))* 60 * 60 * 24),2)
    end
    where t.userid is null
    and t.proxy_ip=t2.proxy_ip
    and t.ip=t2.ip
    and t.rn=t2.rn-1;
    commit;
  end loop;
end;

你可能感兴趣的:(Oracle与数据处理,select)