我在桌子上有这些数据
SIP DATE LOGIN LOGOUT
237 2017-10-31 10:24:51 10:41:35
611 2017-10-31 10:45:32 10:45:40
258 2017-10-31 10:46:03 12:01:57
这是我的记录集,
我想计算SIP在系统上注册的时间。 州R =注册U =未注册但我没有定期登录和注销
我想要:
<meta name="apple-mobile-web-app-capable" content="yes">
并计算保持连接的总时间
感谢您献给我的时间
答案 0 :(得分:0)
以下解决方案将起作用:
select sip,
min(time) as login,
(case when max(time)=min(time) then 'lost'
else max(time)
end
) as logout
from SIPregistration
where time between '10:00:00' and '11:00:00'
and state in('R','U')
group by sip;
如果THEN
没有退出,您可以将sip
之后的文字放在您要显示的内容上。
答案 1 :(得分:0)
您希望结果重叠您的范围。
首先创建SIP范围:
SELECT s1.sip,
s1.time as login
s2.time as logout
COALESCE(s2.time, NOW()) as not_null_logout
FROM SIPregistration s1
LEFT JOIN SIPregistration s2 -- I assume there are some SIP without logout
ON s1.soip =s2.sip
AND s1.time > s2.time
-- WHERE s2.time IS NOT NULL -- optional
现在发现SIP范围是否与您的时间窗口重叠
SELECT s1.sip,
s1.time as login
s2.time as logout
FROM SIPregistration s1
LEFT JOIN SIPregistration s2
ON s1.soip =s2.sip
AND s1.time > s2.time
WHERE s1.time < '11:00:00'
AND s2.time > '10:00:00' -- sip without logout wont show becasue s2.time is NULL.
答案 2 :(得分:0)
最终查询应如下所示:
SELECT sip, `date`
min(time) as login,
IF(max(time) != min(time), max(time), 'Not Logged Yet In This Range') as logout,
IF(max(time) != min(time), TIMEDIFF(max(time), min(time)), 'Not Logged Yet In This Range') as connected
From words
WHERE time between '10:00:00' and '11:00:00'
GROUP BY sip;
您可以使用所需内容更改if子句中的文本。 我们只检查在where子句
中特定范围内的最大时间是否等于minIF(expression ,expr_true, expr_false);
DATEDIFF(interval, date1, date2); the interval not required