简体   繁体   English

如何从两个不同的表中查找两列的MIN和MAX?

[英]How to find MIN and MAX of two columns from TWO different tables?

TABLE1 contains the time of the first call made by an agent and the time of the last call made by an agent for every day GROUPED BY agent name. TABLE1 包含代理商每天对GROUPED BY代理商名称进行的首次呼叫时间和代理商最后一次呼叫的时间。

AGENT      FIRSTCALL           LASTCALL
A         8/5/2013 10:59    8/5/2013 19:50
A         8/6/2013 11:06    8/6/2013 19:50
B         8/5/2013 10:33    8/5/2013 10:35
C         8/5/2013 10:04    8/5/2013 9:56
C         8/6/2013 10:02    8/6/2013 9:47
D         8/5/2013 10:37    8/5/2013 18:47
D         8/6/2013 14:58    8/6/2013 18:19
D         8/6/2013 10:01    8/6/2013 9:59
E         8/5/2013 12:29    8/5/2013 18:51
E         8/6/2013 12:05    8/6/2013 18:48
F         8/6/2013 11:15    8/6/2013 19:49
G         8/5/2013 10:04    8/5/2013 10:09
G         8/6/2013 10:39    8/6/2013 9:54

TABLE2 consists of agent name and daily login and logout time for that agent. 表2 由该代理的名称以及该代理的每日登录和注销时间组成。

AGENT            LOGIN               LOGOUT
A        2013-08-05 11:02:52    2013-08-05 20:05:45
A        2013-08-06 11:00:30    2013-08-06 20:06:47
B        2013-08-05 08:59:07    2013-08-05 18:01:58
B        2013-08-06 09:11:43    2013-08-06 18:08:49
C        2013-08-05 08:58:21    2013-08-05 17:59:29
C        2013-08-06 08:59:13    2013-08-06 18:03:53
D        2013-08-05 10:37:55    2013-08-05 19:56:20
D        2013-08-06 10:37:04    2013-08-06 20:00:43
E        2013-08-06 09:20:50    2013-08-06 18:00:35
F        2013-08-05 10:58:06    2013-08-05 20:00:24
F        2013-08-06 10:49:19    2013-08-06 20:01:37
G        2013-08-06 11:00:17    2013-08-06 19:58:31
H        2013-08-05 09:00:38    2013-08-05 18:16:16
H        2013-08-06 08:56:38    2013-08-06 17:57:00

I need to join these tables in such a manner that I get AGENT, FIRSTCALL, LASTCALL, LOGIN, LOGOUT for each day. 我需要以这样的方式加入这些表,以便每天获得代理,FIRSTCALL,LASTCALL,LOGIN和LOGOUT。 In the example above, I've only listed data for two days but the data exists for over two years (>700 days). 在上面的示例中,我只列出了两天的数据,但是数据存在了两年(> 700天)。

Previously, I've tried connecting these tables via 'AGENT' but that results in multiple records. 以前,我尝试过通过“ AGENT”连接这些表,但这会导致多个记录。 What is the way out? 出路是什么?

something like this will work. 这样的事情会起作用。 May be there'is some better way to convert timestamp to date. 可能存在将时间戳转换为日期的更好方法。

select T1.AGENT, T1.FIRSTCALL, T1.LASTCALL, T2.LOGIN, T2.LOGOUT
from Table1 T1
    inner join Table2 T2 on
        T2.AGENT = T1.AGENT and
        to_char(T1.FIRSTCALL, 'YYYYMMDD') = to_char(T2.LOGIN, 'YYYYMMDD')

see sql fiddle with example 参见sql小提琴与示例

select T1.AGENT, T1.FIRSTCALL, T1.LASTCALL, T2.LOGIN, T2.LOGOUT
from Table1 T1
    inner join Table2 T2 on
        T2.AGENT = T1.AGENT and
        to_char(T1.FIRSTCALL, 'YYYYMMDD') = to_char(T2.LOGIN, 'YYYYMMDD')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM