【问题标题】:select events happening at the same time选择同时发生的事件
【发布时间】:2016-10-19 15:32:22
【问题描述】:

我想要做的是从表中选择在同一日期、时间和地点发生的所有事件,并像这样输出它们:

Event ID    Event ID in conflict
1           2
1           3
1           4
5           6

因此,如果事件 2 startTime 介于事件 1 startTime 和 endTime 之间,它应该出现在列表中。

我在表格中有这些信息:

  • 事件 ID (ID)
  • 事件名称(名称)
  • 开始日期 (startDate)
  • 结束日期 (endDate)
  • 开始时间(startTime)
  • 结束时间 (endTime)
  • 事件位置 ID (locationID)

关于如何在一个 SELECT 中执行此操作的任何建议?提前致谢。

【问题讨论】:

  • 您能展示一下您已经完成的工作吗?
  • 请阅读this,了解一些改进问题的技巧。

标签: sql-server tsql


【解决方案1】:

您可以使用连接获得成对的事件。要获得任何重叠:

select e.eventid, e2.eventid
from events e join
     events e2
     on e.starttime < e2.endtime and
        e.endtime > e2.starttime and
        e.eventid < e2.eventid;

实际上,您的具体问题与重叠无关。因此,对于您的具体问题:

select e.eventid, e2.eventid
from events e join
     events e2
     on e2.starttime between e.starttime and e.endtimee;

但是,我怀疑更通用的重叠逻辑可能适合您的需求。

【讨论】:

    猜你喜欢
    • 2011-04-20
    • 2017-11-28
    • 1970-01-01
    • 2012-08-15
    • 2021-05-02
    • 2019-06-05
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多