gpt4 book ai didi

SQL:高效计算花费的时间

转载 作者:行者123 更新时间:2023-11-29 12:42:05 27 4
gpt4 key购买 nike

给定一个事件时间戳列表,你如何计算“花费的时间”? “花费的时间”是每个时间戳之间的时间间隔的总和,忽略超过某个阈值的间隔,例如1 分钟,在此期间用户可能处于非事件状态。

我试过这样的:

select sum(finish - start)
from (select start.time as start,
finish.time as finish
from events start, events finish
where start.time < finish.time and
finish.time - start.time < interval '1 minute'
group by start.time) as intervals;

但这被证明效率极低,而且从未终止。

最佳答案

使用:

SELECT SUM ( time_interval_up_to_1_min )
FROM (
SELECT
CASE WHEN time - lag( time ) over (ORDER BY time)
<= interval '1' minute
THEN time - lag( time ) over (ORDER BY time)
END As time_interval_up_to_1_min
FROM events
) x

演示:http://sqlfiddle.com/#!17/6f814/7

关于SQL:高效计算花费的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com