表中数据以下:
请求相邻2条纪录 如第1条和第2条纪录建立时候差统计出来
即
zhouhui 5秒
dingxiang 24秒
需求出来了须要处理,厥后找到处理办法了
要领 1:
select t.username,(max( t.CREATIONDATE)-min(t.CREATIONDATE))*24*60*60,count(t.username)/2
from ofloginlog t
--where USERNAME = 'zhouhui'
group by t.username
经由过程分组 统计出用户在线时长(即前后2条纪录作差)
效果图:
申明 末了一个字段我是用来统计 用户登录次数运用的。
oracle 两个时候相减默许的是天数
oracle 两个时候相减默许的是天数*24 为相差的小时数
oracle 两个时候相减默许的是天数*24*60 为相差的分钟数
oracle 两个时候相减默许的是天数*24*60*60 为相差的秒数
要领2:
select username, sum(b), count(username) / 2
from (select id, username, (CREATIONDATE - lgtime) * 24 * 60 * 60 as b
from (select t.*,
lag(type) over(partition by username order by CREATIONDATE) lgtype,
lag(CREATIONDATE) over(partition by username order by CREATIONDATE) lgtime
from ofloginlog t))
-- where USERNAME = 'zhouhui')
group by username
完成效果 一样 这里不帖了
又温习了一下基础的SQL 了 呵呵
20100520 需求有些变动 请求统计个数不是统计TYPE 1 和0 纪录之和均值,只统计TYPE=0 的值,
如许SQL 的分组就不能如许了,想了一下改进了SQL
select g.username, g.time, h.count
from (select t.username,
floor((max(t.CREATIONDATE) - min(t.CREATIONDATE)) * 24 * 60 * 60) as time
from ofloginlog t, ofuser b
where 1 = 1
and t.username = b.username
group by t.username) g,
(select t.username, count(t.username) as count
from ofloginlog t
where t.type = '0'
group by t.username) h
where g.username = h.username
order by count desc
查询效果
剖析 时候差是2个鸠合之间的差,而背面统计个数只是零丁限定前提是TYPE=0的纪录数,统计的数据个数就不一致,所以很难一个分组完成,思绪是先完成 USERNAME 和TIME 的纪录 在统计USERNAME和满足TYPE=0的纪录个数 将2个效果兼并 经由过程 SELECT XX FROM A B 2个暂时表的内联关联完成兼并效果鸠合
本文讲解了sql 相邻2条纪录时候差比较 ,更多相干内容请关注ki4网。
相干引荐:
.net2.0衔接Mysql5数据库设置
cookie 和session 的区分详解
以上就是sql 相邻2条纪录时候差比较的细致内容,更多请关注ki4网别的相干文章!