我试图找到每个状态所花费的总时间,给定一系列值转换。这里有一个例子:
输入:
时间 | |
---|---|
10:00:00 | A |
10:00:01 | A |
10:00:02 | B |
10:00:03 | B |
10:00:05 | A |
10:00:07 | B |
10:00:08 | A |
10:00:09 | B |
10:00:20 | A |
10:00:21 | B |
您可以使用分析函数lead((来引用下一行的时间。
select state, sum(lap) from (
select
time,
state,
lead(time) over(order by time) - time as lap
from tablenane
)
group by state