简体   繁体   English

MYSQL:在单个语句中选择多个if(field ='x'max(time)/ if(field ='y'min(time))值

[英]MYSQL: select multiple if(field='x' max(time) / if(field='y' min(time)) values in a single statement

I am trying to get the below select statement to work. 我试图使下面的选择语句起作用。 Essentially I am trying to get a single row per agent & date that contains the min(time) where agent-state = 'Logged-in', the max(time) where agent-state = 'Logout', the timediff(max(time),min(time)) and the sum(duration) where agent-state = 'Ready'. 从本质上讲,我试图为每个代理&日期获取一行,其中包含min(time)其中agent-state ='Logged-in',max(time)其中agent-state ='Logout',timediff(max( time),min(time))和总和(持续时间),其中agent-state ='Ready'。

My current SQL statement is not complete to this, as I started with just getting the correct min/max time values and hit a wall. 我目前的SQL语句还不完整,因为我首先只是获得正确的最小/最大时间值,然后碰壁。 As it sits now, the Logged-in min time is returned, but not the logout max time. 现在,返回的是已登录的最短时间,但未返回注销的最长时间。

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 Statement: 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;

Thanks for any assistance or advice. 感谢您的协助或建议。

Jim 吉姆

You can't do it this way. 你不能这样子。 The reason you're getting a null for the "other" column is because that agent_state doesn't exist when you group by agent and date. 之所以对“其他”列获取空值,是因为当您按代理商和日期分组时,该agent_state不存在。 If you add the agent_state to the group-by you'll get 3 rows per agent/date. 如果将agent_state添加到分组依据,则每个代理/日期将获得3行。 This isn't quite what you want. 这不是您想要的。

You need a subquery or a couple self-joins. 您需要一个子查询或一对自联接。 Try something like this: 尝试这样的事情:

SELECT agent,
       date,
       MIN(dur)                       AS total_duration,
       TIMEDIFF(MIN(maxt), MIN(mint)) AS time_difference_between_login_and_out,
       MIN(maxt)                      AS logout_time,
       MIN(mint)                      AS login_time
FROM   (SELECT agent,
               date,
               IF(agent_state = 'Ready', SUM(time), NULL)     AS dur,
               IF(agent_state = 'Logged-in', MIN(time), NULL) AS mint,
               IF(agent_state = 'Logout', MAX(time), NULL)    AS maxt,
               agent_state
        FROM   t_cisco_agent_state_details
        GROUP  BY agent,
                  date,
                  agent_state) AS subq
GROUP  BY agent,
          date 

The subquery creates a single row per agent-state, agent and date. 子查询针对每个座席状态,座席和日期创建一行。 Then you can pull your times out of that with the outer query which groups by just agent and date. 然后,您可以使用外部查询(仅按代理商和日期进行分组)来节省时间。

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

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