Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算SQL中具有计数器值的行之间的差异_Sql_Sqlite - Fatal编程技术网

计算SQL中具有计数器值的行之间的差异

计算SQL中具有计数器值的行之间的差异,sql,sqlite,Sql,Sqlite,我得到了表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

我得到了表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
我想要的是计算值1,值10作为

(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

这对我来说太复杂了:
请帮忙。

你的问题有点不清楚。离这儿近吗

DECLARE @Id int = 1

DECLARE @LastCounter1 int,
        @LastCounter10 int,
        @LastTime timestamp,

SELECT TOP 1    @LastCounter1 = counter1,
                @LastCounter10 = counter10,
                @LastTime = unix_time_stamp
FROM            T2
WHERE           id = @Id
ORDER BY        unix_time_stamp DESC

INSERT INTO T2 (id, unix_time_stamp, counter1, counter10, value1, value10)
SELECT      unix_time_stamp,
            counter1,
            counter10,
            ((counter1 - @LastCounter1) / (unix_time_stamp - @LastTime)),
            ((counter10 - @LastCounter10) / (unix_time_stamp - @LastTime))
最新答复:

INSERT INTO T2 (id, unix_time_stamp, counter1, counter10, value1, value10)
SELECT      T1.id,
            T1.unix_time_stamp,
            T1.counter1,
            T1.counter10,
            ((T1.counter1 - [Last].counter1) / (T1.unix_time_stamp - [Last].unix_time_stamp)), 
            ((T1.counter10 - [Last].counter10) / (T1.unix_time_stamp - [Last].unix_time_stamp))
FROM        T1
INNER JOIN  (
                SELECT TOP 1    id,
                                counter1,
                                counter10,
                                unix_time_stamp
                FROM            T2
                WHERE           id = 1
                ORDER BY        unix_time_stamp DESC
            ) [Last] ON T1.id = [Last].id
WHERE       T1.id = 1

我猜下面的查询将计算insert子句所需的所有数据:

SELECT 1e0 * (cur.counter1 - prv.counter1)/(cur.unix_time_stamp - prv.unix_time_stamp)   AS [value1]
, 1e0 * (cur.counter10 - prv.counter10)/(cur.unix_time_stamp - prv.unix_time_stamp)      AS [value10]
, cur.counter1 AS [cur_counter1], cur.counter10 AS [cur_counter10], cur.unix_time_stamp AS [cur_time]
, prv.counter1 AS [prv_counter1], prv.counter10 AS [prv_counter10], prv.unix_time_stamp AS [prv_time]
FROM T1 cur, T1 prv
WHERE cur.counter1 = (SELECT MAX(aux_0.counter1) FROM T1 aux_0)
AND prv.counter1 = (SELECT MAX(aux_1.counter1) FROM T1 aux_1 WHERE aux_1.counter1 < cur.counter1);

你的问题不清楚,请更清楚地告诉我你真正想要的是什么,希望现在能更清楚。是的,很接近,谢谢,但我只是编辑了我的帖子以便更清楚。我正在使用sqlite,但这看起来像t-sql,希望我能找到一种方法来翻译它:这正是我想要的:THX!嗯,选择1e0是什么意思?什么是aux_0。。我只是不明白,这对我来说太复杂了。因为计数器字段是整数,所以得到的表达式将四舍五入为INT;1e0*exp用于将结果表达式类型转换为float,我想您也可以使用1.0*exp。在子查询中使用aux_0来获取maxcounter1时,它是表T1的一个简单别名。尽管在本例中不是强制性的,但我喜欢始终为表别名,并明确标识字段来自哪个表。