根据SQL时间戳确定用户访问时长

发布于 2024-07-10 04:45:27 字数 480 浏览 9 评论 0原文

我分为使用 C# 实现和纯 SQL 2005/2008 实现。 我需要根据他们上次操作的时间戳来确定使用的长度。 我的表包含“friendID”(uniqueIdentifier)和“lastAction”日期时间。 下面是一个示例:

Bob, 1:00 PM 
Bob, 1:01 PM 
Bob, 1:20 PM 
Bob, 1:25 PM 
Jack, 5:00 PM
Bob, 11:20 PM

如果用户有 60 分钟不活动,我想“超时”该用户。 如果您的建议正确有效,则上面的数据应该有 3 个单独的会话。 Bob 下午 1 点开始的会话和晚上 11:20 开始的会话之间有很长的休息时间。

正确的结果应该是:Bob(持续时间 25 分钟)、Jack(持续时间 0 分钟)、Bob(持续时间 0 分钟)

如何使用纯 SQL 或 C# 返回这 3 行?

非常感谢你的帮助,这里真的需要它。

I am divided between using a C# implementation and a pure SQL 2005/2008 implementation. I need to determine the length of usage based on timestamps of their last actions. My table contains "friendID" (uniqueIdentifier) and "lastAction" datetime. Here is an example:

Bob, 1:00 PM 
Bob, 1:01 PM 
Bob, 1:20 PM 
Bob, 1:25 PM 
Jack, 5:00 PM
Bob, 11:20 PM

I want to "time out" a user if they had 60 minutes of inactivity. If your suggestion works correctly, there should be 3 separate sessions from the data above. There is a long break between Bob's session that started at 1 PM and the one that started at 11:20 PM.

Correct results should be: Bob (duration 25 minutes), Jack (duration 0 minutes), Bob (duration 0 minutes)

How would you return those 3 rows using pure SQL or C#?

Thank you very much for your help, it is really needed here.

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

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

发布评论

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

评论(3

思念满溢 2024-07-17 04:45:27

这是一个纯 SQL 示例:

如果您只需要会话的开始时间:

    select u, t       
    from test a        
    where not exists (
      select 1
      from test b
      where a.u = b.u
      and b.t >= a.t - '60 minutes'::interval
      and b.t 

如果您确实需要会话的持续时间:

    select a.u, a.t, c.t
    from test a, test c
    where not exists (
      select 1
      from test b
      where a.u = b.u
      and b.t >= a.t - '60 minutes'::interval
      and b.t  c.t)
    and a.u = c.u
    and a.t 

Here's a pure SQL example:

If you only need the start times of the session:

    select u, t       
    from test a        
    where not exists (
      select 1
      from test b
      where a.u = b.u
      and b.t >= a.t - '60 minutes'::interval
      and b.t 

If you really need the duration of the session:

    select a.u, a.t, c.t
    from test a, test c
    where not exists (
      select 1
      from test b
      where a.u = b.u
      and b.t >= a.t - '60 minutes'::interval
      and b.t  c.t)
    and a.u = c.u
    and a.t 
叫思念不要吵 2024-07-17 04:45:27

这听起来像是一个简单的问题,除非我误解了这个问题:

select friendId, max(lastAction) 'lastAction'
  from myTable
  where lastAction >= dateadd(day, -1, getdate())
      -- don't analyze data over 24 hours old
  group by friendId
  having max(lastAction) < dateadd(minute, -60, getdate())

这将返回在过去 24 小时内(但不是在过去 60 分钟内)进行过活动的所有朋友。

It sounds like a simple problem, unless I'm misunderstanding the question:

select friendId, max(lastAction) 'lastAction'
  from myTable
  where lastAction >= dateadd(day, -1, getdate())
      -- don't analyze data over 24 hours old
  group by friendId
  having max(lastAction) < dateadd(minute, -60, getdate())

This returns all friends who have had activity in the last 24 hours, but not in the last 60 minutes.

┈┾☆殇 2024-07-17 04:45:27

记录会话中第一个操作的开始时间。 还要记录会话中的最后一次时间。 查看 Globals.asax 事件。 应该有类似 OnSessionEnd 的内容(如果有人知道确切的事件,请评论)。 发生这种情况时,记录开始时间和上次之间的持续时间。 如果您不使用 ASP.Net,那么 WinForms 中应该有等效的事件。

现在,处理 60 分钟超时的最简单方法就是在 web.config 中设置它。 如果您不想这样做,那么在保存当前日期时必须有一些逻辑来处理这个问题。 例如,如果新的当前日期距离旧日期超过 60 分钟,则记录旧的持续时间,然后将开始日期和当前日期重置为现在。

Record the start time of the first action in the session. Record the last time in the session as well. Look at te Globals.asax events. There should be something like OnSessionEnd (If anyone knows the exact event, please comment). When this happens, record the durration between start time and last time. If you are not using ASP.Net, then there should be equivalent events in WinForms.

Now, the easiest way to deal with the 60 minute timout is to just set it in the web.config. If you don't want to do that, then you will have to have some logic when you are saving your current date to handle this. Like, if the new current date is more than 60 minutes from the old one, then record the old durration and then reset the start and current dates to now.

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