微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在MS Access中编写查询以对与其他记录的时间段重叠的记录进行分类?

如何解决如何在MS Access中编写查询以对与其他记录的时间段重叠的记录进行分类?

我有一个如下的中间表

Ref#    |Time                   |id
--------------------------------------
10      |10/23/2020 6:48:03 PM  |9
10      |10/23/2020 6:53:56 PM  |12  <-- target output record
10      |10/23/2020 7:53:56 PM  |9
12      |10/23/2020 7:48:03 PM  |11
12      |10/23/2020 7:55:56 PM  |11

我的目标是在给定相同Ref#的情况下,对属于其他ID的时间轴的所有不同ID进行分类。在上述情况下,id 12与id 9的时间段重叠。非常感谢。

解决方法

嗯。 。 。您似乎想要介于另一个ID之间的任何时间。所以,我在想:

select t.*
from t
where exists (select 1
              from t as t2
              where t2.ref = t1.ref and
                    t2.id <> t.id
              group by t2.id
              having t.time > min(t2.time) and
                     t.time < max(t2.time)
             );

或者使用group by可能更有效:

select t.*
from t inner join
     (select ref,id,min(time) as min_time,amx(time) as max_time
      from t
      group by ref,id
     ) as tt
     on t.ref = tt.ref and
        t.id <> tt.id and
        t.time > tt.min_time and
        t.time < tt.max_time;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。