我得到了表 T1,我在其中更新了一些计数器值
id, unix_time_stamp, counter1, counter10
1 , 1333435800 , 55 , 80
然后我得到了表 T2,我在其中复制了这些值
id, unix_time_stamp, counter1, counter10, value1, value10
1 , 1333435800 , 55 , 80 , 0 , 0
2 , 1333435801 , 60 , 87 , 5 , 7
3 , 1333435802 , 70 , 90 , 10 , 3
3 , 1333435804 , 80 , 100 , 5 , 5
这是通过一些触发功能完成的
INSERT INTO T2 (unix_time_stamp, counter1, counter10) SELECT unix_time_stamp, counter1, counter10 FROM T1 WHERE id=1
我想要的是计算 value1, value10 作为
(current_counter1 - last_counter1)/(current_time - last_time)
并将它们放入此插件中。
例如时间戳为 1333435804 的值 1 将是
value1=(80-70)/(1333435804-1333435802) = 5
也就是说
insert into t2
(unix_time_stamp, counter1, counter10, value1)
SELECT unix_time_stamp, counter1, counter10,
(counter1 - (select counter1 from T1 order by unix_time_stamp DESC LIMIT 1)/
(unix_time_stamp - (select unix_time_stamp from T1 order by unix_time_stamp DESC LIMIT 1)
FROM T1 WHERE id=1
但我想要一个更短的版本,因为我有 10 个计数器 :)
整个情况有点复杂,我有理由不在 SQL 之外这样做
我正在使用 sqlite
这对我来说太复杂了:)请帮忙。