mysql更新并设置当前行最后8小时的总和值
mysql update and set sum value of last 8 hours of the current row
你好我想统计当前数据
chat_duration从8小时前的总和
我有:
agent
文字
start_time
日期时间
end_time
日期时间
chat_duration
bigint
我需要将计算结果插入past8_hours_chat_duration
所以当我有:
+----+--------+------------+----------+---------------+---------------------------+
| id | agent | start_time | end_time | chat_duration | past8_hours_chat_duration |
+----+--------+------------+----------+---------------+---------------------------+
| 1 | agent1 | 00.00.00 | 00.01.00 | 60 | |
| 2 | agent2 | 00.00.00 | 00.01.00 | 60 | |
| 3 | agent1 | 00.02.00 | 00.04.00 | 120 | |
| 4 | agent1 | 08.02.00 | 08.03.00 | 60 | |
+----+--------+------------+----------+---------------+---------------------------+
我会尽量解释清楚。
对于每一行,我需要找到当前代理过去 8 小时的持续时间总和
或者换句话说:start_time
在(currentData.start_time
减去 8 hour
)之后而不是它本身(当前行)而不是 start_time
在 currentData.start_time
之后
对于 id 1,agent1
没有会话,其中 start_time
在 00.00.00
减去 8 小时(当前 start_time)之后,因此总数为 0
对于 id 2,agent2
也没有会话,其中 start_time
在 00.00.00
减去 8 小时(当前 start_time)之后,因此总数为 0
对于 id 3,因为 id 1 的 start_time > 00.02.00(当前)- 8 小时所以总数是 60
和
对于 id 4,因为 start_time 的
id 1 < 08.02.00(当前)- 8 小时
& id 3 is > 08.02.00(current) - 8 小时
所以总数是 120(来自 id 3)
我正在使用 mysql
一开始我用的是:
UPDATE chats AS c
JOIN ( SELECT agent,
SUM(chat_duration) AS sum_duration
FROM abc
GROUP BY agent
) AS c2
ON c2.agent = c.agent
SET c.past8_hours_chat_duration = c2.sum_duration
WHERE c.id < 10;
但这是所有代理持续时间的总和,我应该如何找到过去 8 小时聊天数据的总和。
谢谢,
您可以在使用相关子查询的查询中执行此操作:
select c.*,
(select sum(c2.duration)
from chats c2
where c2.agent = c.agent and
c2.start_time > c.start_time - interval 8 hour and
c2.start_time <= c.start_time
) as past8_hours_chat_duration
from chats c;
在 MySQL 中,将其集成到 update
中是棘手的,因为您只能引用 join
子句中正在更新的 table。所以:
update chats c join
(select c.*,
(select sum(c2.duration)
from chats c2
where c2.agent = c.agent and
c2.start_time > c.start_time - interval 8 hour and
c2.start_time <= c.start_time
) as past8_hours_chat_duration
from chats c
) cc
on c.id = cc.id
c.past8_hours_chat_duration = coalesce(cc.past8_hours_chat_duration, 0);
你好我想统计当前数据
chat_duration从8小时前的总和
我有:
agent
文字
start_time
日期时间
end_time
日期时间
chat_duration
bigint
我需要将计算结果插入past8_hours_chat_duration
所以当我有:
+----+--------+------------+----------+---------------+---------------------------+ | id | agent | start_time | end_time | chat_duration | past8_hours_chat_duration | +----+--------+------------+----------+---------------+---------------------------+ | 1 | agent1 | 00.00.00 | 00.01.00 | 60 | | | 2 | agent2 | 00.00.00 | 00.01.00 | 60 | | | 3 | agent1 | 00.02.00 | 00.04.00 | 120 | | | 4 | agent1 | 08.02.00 | 08.03.00 | 60 | | +----+--------+------------+----------+---------------+---------------------------+
我会尽量解释清楚。
对于每一行,我需要找到当前代理过去 8 小时的持续时间总和
或者换句话说:start_time
在(currentData.start_time
减去 8 hour
)之后而不是它本身(当前行)而不是 start_time
在 currentData.start_time
之后
对于 id 1,agent1
没有会话,其中 start_time
在 00.00.00
减去 8 小时(当前 start_time)之后,因此总数为 0
对于 id 2,agent2
也没有会话,其中 start_time
在 00.00.00
减去 8 小时(当前 start_time)之后,因此总数为 0
对于 id 3,因为 id 1 的 start_time > 00.02.00(当前)- 8 小时所以总数是 60
和 对于 id 4,因为 start_time 的 id 1 < 08.02.00(当前)- 8 小时 & id 3 is > 08.02.00(current) - 8 小时 所以总数是 120(来自 id 3)
我正在使用 mysql 一开始我用的是:
UPDATE chats AS c
JOIN ( SELECT agent,
SUM(chat_duration) AS sum_duration
FROM abc
GROUP BY agent
) AS c2
ON c2.agent = c.agent
SET c.past8_hours_chat_duration = c2.sum_duration
WHERE c.id < 10;
但这是所有代理持续时间的总和,我应该如何找到过去 8 小时聊天数据的总和。
谢谢,
您可以在使用相关子查询的查询中执行此操作:
select c.*,
(select sum(c2.duration)
from chats c2
where c2.agent = c.agent and
c2.start_time > c.start_time - interval 8 hour and
c2.start_time <= c.start_time
) as past8_hours_chat_duration
from chats c;
在 MySQL 中,将其集成到 update
中是棘手的,因为您只能引用 join
子句中正在更新的 table。所以:
update chats c join
(select c.*,
(select sum(c2.duration)
from chats c2
where c2.agent = c.agent and
c2.start_time > c.start_time - interval 8 hour and
c2.start_time <= c.start_time
) as past8_hours_chat_duration
from chats c
) cc
on c.id = cc.id
c.past8_hours_chat_duration = coalesce(cc.past8_hours_chat_duration, 0);