我创建了以下架构:
CREATE TABLE test (
id INT,
stat_id INT,
time DATETIME
);
INSERT INTO test (id, stat_id, time) VALUES (1, 1, '2020-09-21 00:02:31');
INSERT INTO test (id, stat_id, time) VALUES (5, 1, '2020-09-21 00:06:31');
INSERT INTO test (id, stat_id, time) VALUES (2, 2, '2020-09-19 00:08:31');
INSERT INTO test (id, stat_id, time) VALUES (3, 2, '2020-09-21 00:03:31');
INSERT INTO test (id, stat_id, time) VALUES (6, 2, '2020-09-23 00:02:31');
INSERT INTO test (id, stat_id, time) VALUES (4, 2, '2020-09-27 00:04:31');
INSERT INTO test (id, stat_id, time) VALUES (7, 3, '2020-09-20 00:04:31');
INSERT INTO test (id, stat_id, time) VALUES (8, 3, '2020-09-23 00:05:31');
https://www.db-fiddle.com/f/6CRv6XqYMAfkBHEBhz1zGe/1
我有3个不同的 stat_id 组。
它们按照ID的顺序排列(从最小到最大)。
我需要找到每组中一个事件到下一个事件之间的平均持续时间。
例如,对于site_id = 2,我需要得到2020-09-21和2020-09-19之间的差值,然后是2020-09-23和2020-09-21,然后是2020-09- 27和2020-09-23。
然后我需要获取每行之间的平均持续时间,最长时间(即2020-09-27和2020-09-23之间的时间)和最短时间。
我需要对所有3个stat_id组执行此操作。
从本质上讲,我平均在看每个stat_id组创建新行所花费的时间。
我尝试过类似的事情:
select
stat_id,
AVG(time) as avg,
timestampdiff(hour, min(time), max(time)) as diff_in_hours,
from test
group by stat_id;
但是显然这是错误的。它给出了错误的平均值,并且仅给出了每个组中最大和最小之间的差异,而这恰恰不是我想要的。我不确定该如何区分上一行和上一行?
答案 0 :(得分:0)
一个选项使用lag()
:
select stat_id, avg(diff) avg_diff
from (
select t.*,
timestampdiff(hour, lag(time) over(partition by stat_id order by id), time) diff
from test t
) t
group by stat_id