根据时间戳加入最后一条记录

发布于 2024-12-09 09:41:13 字数 146 浏览 1 评论 0原文

我有一个访问日志,它记录应用程序中各个点的用户名和时间戳。我想看看我是否可以估算出他们在申请中花费的时间。基本上我想根据时间戳将每个记录连接到最后一个访问记录,这样我就可以计算出时间差。

我想不出如何加入时间戳小于当前记录的第一条记录。有什么方法可以做到这一点?

I have a access log, which records usernames and a timestamp at various points in the application. I want to see if I can approximate the amount of time they are spending in the application. Basically I want to join each record to the last access record based on the timestamp, so I can figure out the time difference.

I can't think of how to join to the first record whose timestamp is less than the current record. What is the way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夜血缘 2024-12-16 09:41:13

尝试这样的事情:

SELECT A.UserName, A.EntryDate, DATEDIFF(ss,A.EntryDate, B.EntryDate) SecondsDuration
FROM YourTable A
OUTER APPLY (SELECT MAX(EntryDate) EntryDate
             FROM YourTable
             WHERE UserName = A.UserName AND EntryDate < A.EntryDate) B

Try something like this:

SELECT A.UserName, A.EntryDate, DATEDIFF(ss,A.EntryDate, B.EntryDate) SecondsDuration
FROM YourTable A
OUTER APPLY (SELECT MAX(EntryDate) EntryDate
             FROM YourTable
             WHERE UserName = A.UserName AND EntryDate < A.EntryDate) B
家住魔仙堡 2024-12-16 09:41:13

我不知道如何连接到时间戳小于当前记录的第一条记录。有什么方法可以做到这一点?

您几乎给了自己答案:

类似于下面的内容应该有效:

SELECT
   lt.*,
   lastentry.timeofEntryColumn,
   DATEDIFF( second, lt.timeofEntryColumn, lt.timeofEtnryColumn) AS DifferenceinSeconds
FROM logTable lt
LEFT OUTER JOIN logtable AS lastentry
ON lastentry.ID = (SELECT TOP 1 ID FROM logtable lt2
          WHERE lt2.timeofEntryColumn < lt.timeofEntryColumn
          ORDER BY lt2.timeofEntryColumn desc)

当然,您应该添加更多内容:也许外连接的一个条件需要 userID 匹配...

I can't think of how to join to the first record whose timestamp is less than the current record. What is the way to do this?

You almost gave yourself the answer:

Something similar to below should work:

SELECT
   lt.*,
   lastentry.timeofEntryColumn,
   DATEDIFF( second, lt.timeofEntryColumn, lt.timeofEtnryColumn) AS DifferenceinSeconds
FROM logTable lt
LEFT OUTER JOIN logtable AS lastentry
ON lastentry.ID = (SELECT TOP 1 ID FROM logtable lt2
          WHERE lt2.timeofEntryColumn < lt.timeofEntryColumn
          ORDER BY lt2.timeofEntryColumn desc)

Of course, you should add some more to this: maybe a condition to the outer join that requires the userID match...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文