基于使用SQL存储在列中的值,以10秒为间隔添加秒数
我们走吧。说表A基于使用SQL存储在列中的值,以10秒为间隔添加秒数,sql,Sql,我们走吧。说表A Time1. Col 10:10:00. 3 10:10:10. 1 10:10:20. 2 结果 Time1 calculated. Col 10:10:00 10:10:10 3 10:10:00 10:10:20 3 10:10:00 10:10:30 3 10:10:10 10:10:20 1 10:10:20 10:10:3
Time1. Col
10:10:00. 3
10:10:10. 1
10:10:20. 2
结果
Time1 calculated. Col
10:10:00 10:10:10 3
10:10:00 10:10:20 3
10:10:00 10:10:30 3
10:10:10 10:10:20 1
10:10:20 10:10:30 2
10:10:20 10:10:40 2
一个简单的方法使用递归CTE。并非所有数据库都使用递归关键字:
with recursive cte (time1, col, n)
select time1, col, 1
from a
union all
select time1, col, n + 1
from cte
where n < col
)
select time1, time1 + (n - 1) * interval '10 second' as calculated, col
from cte;
用您正在使用的数据库标记您的问题。
select a.time1,
a.time1 + (n.n - 1) * interval '10 second' as calculated,
a.col
from a join
(select row_number() over (order by time1) as n
from a
) n
on n.n <= a.col;