计算不同行上两个日期之间的时间间隔

发布于 2024-08-13 10:57:34 字数 1357 浏览 2 评论 0原文

我有登录和注销事件,我需要计算它们之间的时间。

我想我可以将每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2024-08-20 10:57:34

鉴于 SQL 没有任何聚合 Diff 方法,我建议将表连接到自身,并从连接的每一侧选择您要查找的每一行。

比如:

var diff = from a in db.Events
           join b in db.Events on a.SessionId equals b.SessionId
           where a.EventType == 'Login' && b.EventType == 'Logout'
           select b.EventTime - a.EventTime;

还没有尝试过这个,但是类似的东西应该可以工作。


编辑:更新以适应新提供的信息。

尝试以下操作。也许可以更简洁,但可以完成工作。为了便于阅读,已分为几个查询。

var query = from a in (from log in data.Elements()
                       select new {
                          date = DateTime.Parse(log.Element("DateTime").Value),
                          msg = log.Element("MessageText").Value
                       })
            select new {
                a.date,
                type = a.msg.Contains("login") ? "Login" : "Logout",
                user = a.msg.Substring(a.msg.LastIndexOf(',') + 1)
            };

var results = from a in query
            join b in query on a.user equals b.user
            where a.type == "Login" && b.type == "Logout"
                && b.date == (query.OrderBy(o => o.date).Where(d => d.date > a.date).FirstOrDefault().date)
            select new {
                a.user,
                Login = a.date,
                Logout = b.date             
            };

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:

var diff = from a in db.Events
           join b in db.Events on a.SessionId equals b.SessionId
           where a.EventType == 'Login' && b.EventType == 'Logout'
           select b.EventTime - a.EventTime;

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.

var query = from a in (from log in data.Elements()
                       select new {
                          date = DateTime.Parse(log.Element("DateTime").Value),
                          msg = log.Element("MessageText").Value
                       })
            select new {
                a.date,
                type = a.msg.Contains("login") ? "Login" : "Logout",
                user = a.msg.Substring(a.msg.LastIndexOf(',') + 1)
            };

var results = from a in query
            join b in query on a.user equals b.user
            where a.type == "Login" && b.type == "Logout"
                && b.date == (query.OrderBy(o => o.date).Where(d => d.date > a.date).FirstOrDefault().date)
            select new {
                a.user,
                Login = a.date,
                Logout = b.date             
            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文