如何使用sqllite获取最近6个小时的数据

时间:2018-11-07 15:41:16

标签: sqlite

代码:

.mode column
.width 40 20 20 6 10 90
.headers off
select name, datetime("start time","unixepoch","localtime"), datetime("end time","unixepoch","localtime"), "end time"-"start time", "completion status", "failed clients list"
from  "savegroup job"
where datetime("end time","unixepoch","localtime") >= datetime("now", "-6 hours")
and "completion status" like "%failed%";

>= datetime("now", "-6 hours")不起作用。

我需要获取最近6个小时的最新输入数据。

谢谢

1 个答案:

答案 0 :(得分:0)

我相信您的问题很可能是开始时间结束时间列中的值未以正确的格式保存。这是为了应用 unixepoch 修饰符,然后值必须采用DDDDDDDDDD格式。

按照:-

  

“ unixepoch”修饰符(11)仅在紧跟在   以DDDDDDDDDD格式的时间字符串。此修饰符导致   DDDDDDDDDD不能正常解释为儒略日数   会是,但是作为Unix Time-自1970年以来的秒数。   “ unixepoch”修饰符不遵循以下格式的时间字符串   DDDDDDDDDD,表示自1970年以来的秒数,如果   其他修饰符将“ unixepoch”修饰符与之前的修饰符分开   DDDDDDDDDD,则行为未定义。对于之前的SQLite版本   3.16.0(2017-01-02),“ unixepoch”修饰符仅适用于0000-01-01 00:00:00到5352-11-01 10:52:47之间的日期(   -62167219200到106751991167)。

SQL As Understood By SQLite - Date And Time Functions - Modifiers

例如,考虑以下内容(请参见注释):-

DROP TABLE IF EXISTS 'savegroup job';
CREATE TABLE IF NOT EXISTS 'savegroup job' (name TEXT,'start time' TEXT, 'end time' TEXT, 'completion status' TEXT);
INSERT INTO 'savegroup job' VALUES

  -- store values in DDDDDDDDDD format
    ('Name001',strftime('%s','2018-01-01 10:30'),strftime('%s','2018-01-01 12:30'),'this failed'),
    ('Name002',strftime('%s','2018-02-01 10:30'),strftime('%s','2018-02-01 12:30'),'this failed'),
    ('Name003',strftime('%s','2018-03-01 10:30'),strftime('%s','2018-03-01 12:30'),'this failed'),
    ('Name004',strftime('%s','now'),strftime('%s','now','+6 hours'),'this failed'),
    ('Name005',strftime('%s','now','+3 hours'),strftime('%s','now','+14 hours'),'this failed'),

    -- store values in "YYYY-MM-DD HH:MM:SS" format 
    ('Name006','2018-01-01 10:30','2018-01-01 12:30','this failed'),
    ('Name007','2018-01-01 10:30','2018-01-01 12:30','this failed'),
    ('Name008','2018-01-01 10:30','2018-01-01 12:30','this failed'),
    ('Name009',datetime('now'),datetime('now','+6 hours'),'this failed'),
    ('Name010',datetime('now','+3 hours'),datetime('now','+14 hours'),'this failed')
;
-- Show all data
SELECT * FROM 'savegroup job';

-- The query from the question
select name, datetime("start time","unixepoch","localtime"), datetime("end time","unixepoch","localtime"), "end time"-"start time", "completion status", "failed clients list"
from  "savegroup job"
where datetime("end time","unixepoch","localtime") >= datetime("now", "-6 hours")
and "completion status" like "%failed%";

结果

所有数据:-

enter image description here

您的查询(如果值以正确的格式存储,它确实可以工作):-

enter image description here

处理当前存储的数据

假设问题是开始时间结束时间列确实以“ YYYY-MM-DD HH:MM:SS”格式存储值(行其中名称为Name006-Name010),则以下查询将起作用:-

-- Modified query from the question
SELECT name, 
    datetime("start time"), 
    datetime("end time"),
    strftime('%s',datetime("end time"))-strftime('%s',datetime("start time")), 
    "completion status", 
    "failed clients list"
FROM  "savegroup job"
-- where strftime('%s',"end time","unixepoch","localtime") >= strftime('%s',"now", "-6 hours") and "completion status" like "%failed%";
WHERE strftime('%s',"end time") >= strftime('%s',datetime('now','-6 hours'))
    AND "completion status" LIKE '%failed%'
;

导致:-

enter image description here