RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1475309
Accepted
Ayo
Ayo
Asked:2022-12-08 20:09:38 +0000 UTC2022-12-08 20:09:38 +0000 UTC 2022-12-08 20:09:38 +0000 UTC

最大时隙交叉数

  • 772

有一个表格列出了事件的开始和结束。事件有不同的类型。我们需要找到同时发生的事件的最大数量。

表格示例:

事件编号 时间_开始 时间结束 事件类型
一 2022-10-01 15:01:21 2022-10-01 15:03:21 一个
2个 2022-10-01 15:02:13 2022-10-01 15:03:10 一个
3个 2022-10-13 14:18:11 2022-10-13 15:07:01 乙
四 2022-10-10 12:04:51 2022-10-10 12:06:28 乙
5个 2022-10-01 15:03:22 2022-10-01 15:05:43 一个

例如,在这个表中,对于类型 A,有一个事件 id 1 和 2 同时发生的时刻,对于类型 B,事件不相交,即 结果将是这样的:

事件类型 最大限度()
一个 2个
乙 一

往哪个方向挖,不清楚:(

BD-垂直

sql
  • 1 1 个回答
  • 28 Views

1 个回答

  • Voted
  1. Best Answer
    Nofate
    2022-12-09T04:14:39Z2022-12-09T04:14:39Z

    让我们创建一个表并将数据加载到其中。我们在这里不会对 ID 感兴趣。

    create table events (
        time_start datetime,
        time_end datetime,
        event_type varchar
    );
    
    insert into events values
    ('2022-10-01 15:01:21', '2022-10-01 15:03:21',  'A'),
    ('2022-10-01 15:02:13', '2022-10-01 15:03:10',  'A'),
    ('2022-10-13 14:18:11', '2022-10-13 15:07:01',  'B'),
    ('2022-10-10 12:04:51', '2022-10-10 12:06:28',  'B'),
    ('2022-10-01 15:03:22', '2022-10-01 15:05:43',  'A');
    

    让我们想想如何在一张纸/日历/谷歌日历上解决这个问题。您可以在日历上的每个事件的开始和结束之间绘制一个条带。不同类型的事件 - 不同的颜色。然后再次从头到尾,记下每个时间点有多少条相同颜色的条纹。在下一个事件开始或结束的地方这样做就足够了。然后从记录的每种事件的总和中找出最大值。

    在此处输入图像描述


    仍然需要在 SQL 中重现这一点。让我们将每个事件分成两行:开始和结束。此外,我们将添加一个列来指示事件数量的变化。开头将指示事件数 +1,结尾为 -1。让我们按时间排序以获得递增的时间序列。

    select time_start as ts, event_type, +1 as cnt from events
    union all
    select time_end   as ts, event_type, -1 as cnt from events
    
    ts  event_type  cnt
    2022-10-01 15:01:21.000000  A   1
    2022-10-01 15:02:13.000000  A   1
    2022-10-01 15:03:22.000000  A   1
    2022-10-10 12:04:51.000000  B   1
    2022-10-13 14:18:11.000000  B   1
    2022-10-01 15:03:21.000000  A   -1
    2022-10-01 15:03:10.000000  A   -1
    2022-10-01 15:05:43.000000  A   -1
    2022-10-10 12:06:28.000000  B   -1
    2022-10-13 15:07:01.000000  B   -1
    

    现在您需要为每一行找到其上方所有 cnt 值的总和,并按个人进行细分event_type。这是通过运行/累积总和完成的。在 Vertica 中 -解析和:

    select ts, event_type, cnt, sum(cnt) OVER (PARTITION BY event_type ORDER BY ts) as cnt_cum from (
        select time_start as ts, event_type, +1 as cnt from events
        union all
        select time_end as ts, event_type, -1 as cnt from events
    ) e1
    
    ts  event_type  cnt cnt_cum
    2022-10-01 15:01:21.000000  A    1  1
    2022-10-01 15:02:13.000000  A    1  2
    2022-10-01 15:03:10.000000  A   -1  1
    2022-10-01 15:03:21.000000  A   -1  0
    2022-10-01 15:03:22.000000  A    1  1
    2022-10-01 15:05:43.000000  A   -1  0
    2022-10-10 12:04:51.000000  B    1  1
    2022-10-10 12:06:28.000000  B   -1  0
    2022-10-13 14:18:11.000000  B    1  1
    2022-10-13 15:07:01.000000  B   -1  0
    

    cnt_cum仍然需要为每个找到最大值event_type:

    select event_type, max(cnt_cum) from (
      select ts, event_type, cnt, sum(cnt) OVER (PARTITION BY event_type ORDER BY ts) as cnt_cum from (
        select time_start as ts, event_type, +1 as cnt from events
        union all
        select time_end as ts, event_type, -1 as cnt from events
      ) e1
    order by ts
    ) e2 group by event_type;
    
    event_type  max
    B   1
    A   2
    
    
    • 2

相关问题

  • 通过 OUT 参数从过程结果输出

  • ON 关键字附近的语法错误 - SQL

  • 多表查询中的 Count() 聚合函数

  • 根据时间更改单元格中的日期

  • phpMyAdmin 中的错误 #1064 SQL 查询

  • Qt:包含变量的数据库查询

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5