如何编写查询以从一个表中选择最接近但小于另一个表中的日期的日期

时间:2019-06-11 16:48:59

标签: sql ms-access

我的Access数据库中有2个表。一个表中列出了通话和通话时间:

|Start Time         |Agent    |
|6/7/2019 9:30:47 AM|Abandoned|
|6/7/2019 3:09:11 PM|Abandoned|
|6/7/2019 9:12:49 AM|Abandoned|
|6/7/2019 8:44:22 AM|Abandoned|
|6/7/2019 8:13:54 AM|Abandoned|
|6/7/2019 7:25:48 AM|Abandoned|

我的其他表格中有一个用户列表,以及他们更改电话状态的时间:

|Agent         |StartTime    |Status           |
|Doe, John     |6/7/2019 7:09|Special          |
|Zombie, Rob   |6/7/2019 7:16|Available        |
|Buffett, Jimmy|6/7/2019 7:18|Training         |
|Buffett, Jimmy|6/7/2019 7:20|Available        |
|Zombie, Rob   |6/7/2019 7:27|Tickets          |
|Zombie, Rob   |6/7/2019 7:29|Available        |
|Buffett, Jimmy|6/7/2019 8:00|Training         |
|Zombie, Rob   |6/7/2019 8:01|Tickets          |
|Buffett, Jimmy|6/7/2019 8:01|Unscheduled Break|
|Doe, Jane     |6/7/2019 8:10|Unscheduled Break|
|Doe, Jane     |6/7/2019 8:14|Available        |
|Buffett, Jimmy|6/7/2019 8:16|Available        |
|Zombie, Rob   |6/7/2019 8:29|Available        |
|Zombie, Rob   |6/7/2019 8:32|Tickets          |
|Lee, Amy      |6/7/2019 8:50|Unscheduled Break|

我想了解的是在呼叫被放弃时每个座席处于什么状态。

例如:7:25:48 AM的放弃呼叫将是:

|Agent         |StartTime    |Status           |
|Doe, John     |6/7/2019 7:09|Special          |
|Zombie, Rob   |6/7/2019 7:16|Available        |
|Buffett, Jimmy|6/7/2019 7:20|Available        |

在8:13:54 AM被放弃的呼叫将是:

|Agent         |StartTime    |Status           |
|Doe, John     |6/7/2019 7:09|Special          |
|Zombie, Rob   |6/7/2019 8:01|Tickets          |
|Buffett, Jimmy|6/7/2019 8:01|Unscheduled Break|
|Doe, Jane     |6/7/2019 8:10|Unscheduled Break|

对于我的一生,我什至不知道从哪里开始。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将两个表连接在一起,然后从被放弃的呼叫中或之前获取时间与最大开始时间匹配的记录:

select t1.*, t2.*
from table1 as t1,  -- only because MS Access does not support CROSS JOIN
     table2 as t2
where t2.starttime = (select max(tt2.starttime)
                      from table2 as tt2
                      where tt2.agent = t2.agent and
                            tt2.starttime <= t1.starttime
                     );