同一聊天中两条消息的响应时间
Response time of two messages in the same chat
场景
一个chat_channel可以有多个MESSAGE_ID显示在不同的行上。
INSERT INTO messages_tbl
(message_id,
chat_channel,
user_type,
message_type,
message_custom_type,
msg_created_on)
VALUES ('00001',
'chat_1',
'consumer',
'msg',
'example message',
'2018-09-27 10:14:42');
INSERT INTO messages_tbl
(message_id,
chat_channel,
user_type,
message_type,
message_custom_type,
msg_created_on)
VALUES ('00002',
'chat_1',
'merchant',
'msg',
'example message',
'2018-09-27 11:14:42');
在这个例子中,我想比较和计算从消费者发送的第一条消息到商家发送的第二条消息的响应时间。有关时间和日期的数据是 MSG_CREATED_ON.
列
我能够进行计算,但因为他们在不同的行上而苦苦挣扎。我曾尝试在 CHAT_CHANNEL 上对它们进行分区,但没有成功..
有什么想法吗?
鉴于您使用的是 MySQL 8+,我们可以使用分析函数。这是一种方法:
WITH cte AS (
SELECT CHAT_CHANNEL, MSG_CREATED_ON,
ROW_NUMBER() OVER (PARTITION BY USER_TYPE ORDER BY MSG_CREATED_ON) rn
FROM MESSAGES_TBL
)
SELECT
t1.CHAT_CHANNEL,
t1.rn AS conversation_number,
TIMESTAMPDIFF(MINUTE, t1.MSG_CREATED_ON, t2.MSG_CREATED_ON) AS diff_in_minutes
FROM cte t1
LEFT JOIN cte t2
ON t1.CHAT_CHANNEL = t2.CHAT_CHANNEL AND
t1.rn = t2.rn AND
t1.USER_TYPE = 'consumer' AND
t2.USER_TYPE = 'merchant'
我使用 ROW_NUMBER
为每个聊天频道的消费者和商家之间的每个配对交换分配一个公共值。然后,我加入了聊天频道,对话也一样。
下面是一种方式 -
SELECT t1.chat_channel,
Timestampdiff(minute, t1.msgcreated_on, t2.messagecreated_on)
FROM (SELECT chat_channel,
Min(msgcreated_on) time1
FROM message_tbl
WHERE t1.user_type = 'CONSUMER'
GROUP BY chat_channel) t1
INNER JOIN (SELECT chat_channel,
Min(msgcreated_on) time2
FROM message_tbl
WHERE t1.user_type = 'MERHCANT'
GROUP BY chat_channel) t2
ON t1.chatchannel = t2.chatchannel
场景
一个chat_channel可以有多个MESSAGE_ID显示在不同的行上。
INSERT INTO messages_tbl
(message_id,
chat_channel,
user_type,
message_type,
message_custom_type,
msg_created_on)
VALUES ('00001',
'chat_1',
'consumer',
'msg',
'example message',
'2018-09-27 10:14:42');
INSERT INTO messages_tbl
(message_id,
chat_channel,
user_type,
message_type,
message_custom_type,
msg_created_on)
VALUES ('00002',
'chat_1',
'merchant',
'msg',
'example message',
'2018-09-27 11:14:42');
在这个例子中,我想比较和计算从消费者发送的第一条消息到商家发送的第二条消息的响应时间。有关时间和日期的数据是 MSG_CREATED_ON.
列我能够进行计算,但因为他们在不同的行上而苦苦挣扎。我曾尝试在 CHAT_CHANNEL 上对它们进行分区,但没有成功..
有什么想法吗?
鉴于您使用的是 MySQL 8+,我们可以使用分析函数。这是一种方法:
WITH cte AS (
SELECT CHAT_CHANNEL, MSG_CREATED_ON,
ROW_NUMBER() OVER (PARTITION BY USER_TYPE ORDER BY MSG_CREATED_ON) rn
FROM MESSAGES_TBL
)
SELECT
t1.CHAT_CHANNEL,
t1.rn AS conversation_number,
TIMESTAMPDIFF(MINUTE, t1.MSG_CREATED_ON, t2.MSG_CREATED_ON) AS diff_in_minutes
FROM cte t1
LEFT JOIN cte t2
ON t1.CHAT_CHANNEL = t2.CHAT_CHANNEL AND
t1.rn = t2.rn AND
t1.USER_TYPE = 'consumer' AND
t2.USER_TYPE = 'merchant'
我使用 ROW_NUMBER
为每个聊天频道的消费者和商家之间的每个配对交换分配一个公共值。然后,我加入了聊天频道,对话也一样。
下面是一种方式 -
SELECT t1.chat_channel,
Timestampdiff(minute, t1.msgcreated_on, t2.messagecreated_on)
FROM (SELECT chat_channel,
Min(msgcreated_on) time1
FROM message_tbl
WHERE t1.user_type = 'CONSUMER'
GROUP BY chat_channel) t1
INNER JOIN (SELECT chat_channel,
Min(msgcreated_on) time2
FROM message_tbl
WHERE t1.user_type = 'MERHCANT'
GROUP BY chat_channel) t2
ON t1.chatchannel = t2.chatchannel