SQLite忽略与WHERE语句一起使用的计算

时间:2013-09-11 03:47:41

标签: sqlite

我正在尝试使用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

1 个答案:

答案 0 :(得分: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;