计算不同行上两个日期之间的时间间隔
我有登录和注销事件,我需要计算它们之间的时间。
我想我可以将每 2 行(每两条消息)分组,然后进行计算,但你会怎么做呢?
我需要查询的 XML 示例:
<Log>
<Message>
<DateTime>2009-12-02 14:38:41</DateTime>
<Priority>Local3.Info</Priority>
<Source_Host>192.168.0.100</Source_Host>
<MessageText>Dec 2 14:38:41 root: login,ng1,192.168.0.160,janis.veinbergs</MessageText>
</Message>
<Message>
<DateTime>2009-12-02 15:28:19</DateTime>
<Priority>Local3.Info</Priority>
<Source_Host>192.168.0.100</Source_Host>
<MessageText>Dec 2 15:30:33 root: logout,ng1,,janis.veinbergs</MessageText>
</Message>
<Message>
<DateTime>2009-12-02 15:29:11</DateTime>
<Priority>Local3.Info</Priority>
<Source_Host>192.168.0.100</Source_Host>
<MessageText>Dec 2 15:31:25 root: login,ng1,192.168.0.160,janis.veinbergs</MessageText>
</Message>
<Message>
<DateTime>2009-12-02 15:58:22</DateTime>
<Priority>Local3.Info</Priority>
<Source_Host>192.168.0.100</Source_Host>
<MessageText>Dec 2 16:00:37 root: logout,ng1,,janis.veinbergs</MessageText>
</Message>
</Log>
谢谢。
I have login and logout events and i need to calculate time between them.
I guess i could group each 2 rows (each two messages) and then do the calculation, but how would you do that?
Example XML i need to query:
<Log>
<Message>
<DateTime>2009-12-02 14:38:41</DateTime>
<Priority>Local3.Info</Priority>
<Source_Host>192.168.0.100</Source_Host>
<MessageText>Dec 2 14:38:41 root: login,ng1,192.168.0.160,janis.veinbergs</MessageText>
</Message>
<Message>
<DateTime>2009-12-02 15:28:19</DateTime>
<Priority>Local3.Info</Priority>
<Source_Host>192.168.0.100</Source_Host>
<MessageText>Dec 2 15:30:33 root: logout,ng1,,janis.veinbergs</MessageText>
</Message>
<Message>
<DateTime>2009-12-02 15:29:11</DateTime>
<Priority>Local3.Info</Priority>
<Source_Host>192.168.0.100</Source_Host>
<MessageText>Dec 2 15:31:25 root: login,ng1,192.168.0.160,janis.veinbergs</MessageText>
</Message>
<Message>
<DateTime>2009-12-02 15:58:22</DateTime>
<Priority>Local3.Info</Priority>
<Source_Host>192.168.0.100</Source_Host>
<MessageText>Dec 2 16:00:37 root: logout,ng1,,janis.veinbergs</MessageText>
</Message>
</Log>
Thankyou.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于 SQL 没有任何聚合 Diff 方法,我建议将表连接到自身,并从连接的每一侧选择您要查找的每一行。
比如:
还没有尝试过这个,但是类似的东西应该可以工作。
编辑:更新以适应新提供的信息。
尝试以下操作。也许可以更简洁,但可以完成工作。为了便于阅读,已分为几个查询。
Given SQL doesn't have any aggregate Diff method, I would suggest joining the table onto itself, and selecting each row that you're after from each side of the join.
Something like:
Haven't tried this, but something along those lines should work.
EDIT: updated to suit new provided info.
Try the following. Could perhaps be more concise, but does the job. Have broken down into a couple of queries for easier reading.