【问题标题】:Avoid multiple counting of overlapped times避免多次计算重叠次数
【发布时间】:2020-05-10 17:11:03
【问题描述】:

我已经计算了多个用户登录会话的 ID 组的 start_time 和 end_time 之间的重叠时间。 ID 对用户来说是唯一的。一个用户可以有来自不同浏览器、设备等的多个会话。

这是我的数据集

row       id       start_time        end_time       overlap_in_seconds
1         1        08:41:27          08:47:26       359 
2         1        08:39:31          08:40:42       71
3         1        08:41:37          08:47:26       349

如果您注意到第 1 行和第 3 行,08:41:37 和 08:47:26 之间的时间重叠时间被计算了两次。

我想要显示结果的方式有 2 个选项:

选项 1:

row        id       start_time        end_time       overlap_in_seconds
 1         1        08:41:27          08:47:26       10 (the extra overlap time from 08:41:27 to 08:41:37)
 2         1        08:39:31          08:40:42       71
 3         1        08:41:37          08:47:26       349

选项 2:

我将此表用作临时表,在外部查询中,当我对 Overlap_in_seconds 进行求和时,我得到的总重叠时间为 430 秒 (10+71+349),而不是 779(349+359+71) .

meeting_id.    correct_overlap
1              430

下面是我的外部查询。

select temp.id as meeting_id, sum(temp.Overlap_in_seconds) as correct_overlap
from temp
group by temp.id

任何想法如何做到这一点?

【问题讨论】:

    标签: sql-server datetime overlap counting


    【解决方案1】:

    我不确定这是否是最快的方法,但我想将每个日期转换为 86400 位数组,每个位置对应于一天中的每一秒,接下来只需加入所有数组这将是每组的秒数。

    【讨论】:

      猜你喜欢
      • 2018-06-22
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多