2

いくつかのカウンター値を更新するテーブル 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を使用しています

これは私にとって非常に複雑です:)助けてください。

4

2 に答える 2

1

あなたの質問は少し不明確です。これはどこかに近いですか?

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
于 2012-04-26T13:32:10.237 に答える
0

次のクエリは、挿入句に必要なすべてのデータを計算すると思います。

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);
于 2012-04-26T13:44:44.950 に答える