0

SQLite クエリを使用して、アラームがいつアクティブになるかを計算しようとしています。開始時刻と戻り止め (アラームがアクティブになるまでのタイムアウトまたは遅延) を秒単位で同じテーブルに保存します。スキーマは次のとおりです。

CREATE TABLE alarm_log (
 scope integer NOT NULL, --REFERENCES scopes_inst(inst)
 start integer NOT NULL, -- seconds
 end integer,
 severity text NOT NULL,
 value text NOT NULL,
 details text,
 raised_by integer,      -- the scope id that raised the alarm
 detent integer,         -- detent in seconds
 PRIMARY KEY (scope, start)
);

そして、簡単な数学を使用してアクティブなアラームを取得するために使用しようとしているクエリを次に示します。

SELECT *,
  (start + detent) AS activated, -- the time that the alarm becomes active
  STRFTIME("%s","now") AS now    -- the current time of the query
FROM alarm_log 
WHERE end IS NULL
AND now > activated;             -- ensure that the alarm is activated

30 秒の戻り止めでアラームを作成しました。開始時刻は 1378870712 であるため、1378870742 でアクティブになるはずです (アクティブ化された列を参照)。

これは、アクティブ化された時刻の 4 秒前に行を返す上記のクエリの例です...

scope       start       end         severity    value                  details           raised_by   detent      activated   now       
----------  ----------  ----------  ----------  ---------------------  ----------------  ----------  ----------  ----------  ----------
4           1378870712              warning     out-of-range too-high  min 500 max 1500  0           30          1378870742  1378870738

同じクエリが、アラームがアクティブ化されてから 2 秒後にその行を返します。

scope       start       end         severity    value                  details           raised_by   detent      activated   now       
----------  ----------  ----------  ----------  ---------------------  ----------------  ----------  ----------  ----------  ----------
4           1378870712              warning     out-of-range too-high  min 500 max 1500  0           30          1378870742  1378870744

最後の行の > を < に反転すると、アクティブでないアラームが表示されるはずです...しかし、これは、ディテント時間が経過する前または後に何も返しません...

これは正常な動作ですか?クエリは私には問題ないようです。読んでくれてありがとう :)

編集:

WHERE句で直接計算して試したことにも言及する必要がありましたが、同じことを行います:(

SELECT *,
  (start + detent) AS activated, -- the time that the alarm becomes active
  STRFTIME("%s","now") AS now    -- the current time of the query
FROM alarm_log 
WHERE end IS NULL
AND STRFTIME("%s","now") > (start + detent);

EDIT2:

ここにいくつかのテストデータがあります:

SQLite スタイル:

scope|start|end|severity|value|details|raised_by|detent
4|1378935271|1378935842|warning|out-of-range too-high|min 500 max 1500|0|600
4|1378935854|1378935876|warning|out-of-range too-high|min 500 max 1500|0|600
4|1378935884||warning|out-of-range too-high|min 500 max 1500|0|600

CSV スタイル

scope,start,end,severity,value,details,raised_by,detent
4,1378935271,1378935842,warning,out-of-range too-high,min 500 max 1500,0,600
4,1378935854,1378935876,warning,out-of-range too-high,min 500 max 1500,0,600
4,1378935884,,warning,out-of-range too-high,min 500 max 1500,0,600
4

1 に答える 1

1

これを試して:

select * from (
  select *, (start + detent) as activated,
    strftime("%s","now") as now
  from alarm_log where end is null
) where cast(now as int) > activated;
于 2013-09-11T05:18:32.707 に答える