我正在尝试使以下 select 语句起作用。本质上,我试图为每个代理和日期获取一行,其中包含代理状态 = '登录'的最小(时间),代理状态 = '注销'的最大(时间),时间差异(最大( time),min(time)) 和 sum(duration) 其中 agent-state = 'Ready'。
我当前的 SQL 语句对此并不完整,因为我刚开始只是获取正确的最小/最大时间值并碰壁。现在返回的是登录的最小时间,而不是注销的最大时间。
AGENT, DATE, TIME, DURATION, AGENT_STATE
Alex, 2013-01-01, 07:25:37, 00:00:00, Logged-in
Alex, 2013-01-01, 07:26:01, 00:24:48, Ready
Alex, 2013-01-01, 07:54:20, 00:21:47, Ready
Alex, 2013-01-01, 08:28:11, 00:03:00, Ready
Alex, 2013-01-01, 08:34:11, 00:06:29, Ready
Alex, 2013-01-01, 08:44:30, 00:05:14, Ready
Alex, 2013-01-01, 08:53:29, 00:06:52, Ready
Alex, 2013-01-01, 09:03:48, 00:12:13, Ready
Alex, 2013-01-01, 09:31:56, 00:36:44, Ready
Alex, 2013-01-01, 09:32:27, 14:58:51, Logout
Alex, 2013-01-01, 10:11:37, 00:00:00, Logged-in
Alex, 2013-01-01, 10:12:26, 00:54:44, Ready
Alex, 2013-01-01, 11:09:28, 00:47:48, Ready
Alex, 2013-01-01, 11:57:33, 00:16:54, Ready
Alex, 2013-01-01, 12:15:15, 00:17:32, Ready
Alex, 2013-01-01, 12:34:44, 00:13:32, Ready
Alex, 2013-01-01, 12:51:04, 00:16:44, Ready
Alex, 2013-01-01, 13:12:36, 00:31:38, Ready
Alex, 2013-01-01, 13:49:46, 00:39:14, Ready
Alex, 2013-01-01, 14:31:42, 00:28:45, Ready
Alex, 2013-01-01, 15:00:27, 14:58:51, Logout
SQL 语句:
select
`agent`,
`date`,
if(`agent_state` = 'Logged-in', min(`time`), null) as `min_login`,
if(`agent_state` = 'Logout', max(`time`),null) as `max_logout`
from
t_cisco_agent_state_details
group by
`agent`, `date`
order by
`agent`, `date`, `time` asc;
感谢您提供任何帮助或建议。
吉姆