MySQL数据过滤

时间:2018-08-09 09:15:49

标签: mysql

Id Conversationid    Agentid    Event       Timestamp
---------------------------------------------------------------------
1      1234          aaditya    ASSIGN      2018-08-02 09:19:50
2      1234          aaditya    REPLY       2018-08-02 09:24:46
3      1234          rehman     ASSIGN      2018-08-02 09:25:39
4      2345           pavan     ASSIGN      2018-08-03 11:24:35
5      2345           pavan     REPLY       2018-08-03 11:25:53
6      3456          sanjay     ASSIGN      2018-08-02 13:19:02
7      3456          sanjay     REPLY       2018-08-02 13:20:33
8      4567           rahul    ASSIGN       2018-08-05 04:49:54
9      4567           rahul    REPLY        2018-08-05 04:55:54
10     4567           rahul    ASSIGN       2018-08-05 04:49:54

我有一个如上所述的数据集。我希望将id的对话ID分配给同时具有分配和答复事件的代理,并且两者的差b / w超过5分钟。 即上面数据集输出中应该是id的1,2以及8和9的详细信息

1 个答案:

答案 0 :(得分:-1)

请在MySQL中使用TIMESTAMPDIFF()尝试以下查询

注意:-id(1,2)之间的时间差小于5分钟。因此我们只能预期id(8,9)。

SELECT assigns.id AS AssignID, replies.id AS ReplyID, assigns.agentid AS Agentid
FROM
(SELECT id,agentid,TIMESTAMP FROM timeD WHERE EVENT = 'assign' GROUP BY TIMESTAMP) AS assigns
INNER JOIN
(SELECT id,agentid,TIMESTAMP FROM timeD WHERE EVENT = 'reply' GROUP BY TIMESTAMP) AS replies
ON assigns.agentid = replies.agentid 
AND TIMESTAMPDIFF(MINUTE,assigns.TIMESTAMP,replies.TIMESTAMP) > 5;