How to get data of last 6 hours using sqllite











up vote
0
down vote

favorite












Code:



.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%";


The >= datetime("now", "-6 hours") does not work.



I need to get the last input data from the last 6 hours.



Thanks










share|improve this question




















  • 1




    Show your table definition and some sample rows from it, and what results you want from that sample. (spaces in column and table names? Eww.)
    – Shawn
    Nov 7 at 16:41















up vote
0
down vote

favorite












Code:



.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%";


The >= datetime("now", "-6 hours") does not work.



I need to get the last input data from the last 6 hours.



Thanks










share|improve this question




















  • 1




    Show your table definition and some sample rows from it, and what results you want from that sample. (spaces in column and table names? Eww.)
    – Shawn
    Nov 7 at 16:41













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Code:



.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%";


The >= datetime("now", "-6 hours") does not work.



I need to get the last input data from the last 6 hours.



Thanks










share|improve this question















Code:



.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%";


The >= datetime("now", "-6 hours") does not work.



I need to get the last input data from the last 6 hours.



Thanks







sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 15:49









forpas

4,0761215




4,0761215










asked Nov 7 at 15:41









Rafael Vilela

1




1








  • 1




    Show your table definition and some sample rows from it, and what results you want from that sample. (spaces in column and table names? Eww.)
    – Shawn
    Nov 7 at 16:41














  • 1




    Show your table definition and some sample rows from it, and what results you want from that sample. (spaces in column and table names? Eww.)
    – Shawn
    Nov 7 at 16:41








1




1




Show your table definition and some sample rows from it, and what results you want from that sample. (spaces in column and table names? Eww.)
– Shawn
Nov 7 at 16:41




Show your table definition and some sample rows from it, and what results you want from that sample. (spaces in column and table names? Eww.)
– Shawn
Nov 7 at 16:41












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I believe that your issue is very likely that the values in the start time and end time columns are not saved in the correct format. That is for the unixepoch modifer to apply then the values MUST be in DDDDDDDDDD format.



As per :-




The "unixepoch" modifier (11) only works if it immediately follows a
timestring in the DDDDDDDDDD format. This modifier causes the
DDDDDDDDDD to be interpreted not as a Julian day number as it normally
would be, but as Unix Time - the number of seconds since 1970. If the
"unixepoch" modifier does not follow a timestring of the form
DDDDDDDDDD which expresses the number of seconds since 1970 or if
other modifiers separate the "unixepoch" modifier from prior
DDDDDDDDDD then the behavior is undefined. For SQLite versions before
3.16.0 (2017-01-02), the "unixepoch" modifier only works for dates between 0000-01-01 00:00:00 and 5352-11-01 10:52:47 (unix times of
-62167219200 through 106751991167).




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



For example consider the following (see comments) :-



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%";


Results



All data :-



enter image description here



Your Query (it does work if the values are store in the correct format) :-



enter image description here



Handle data as currently stored



Assuming that the issue is that columns start time and end time do have values store as in "YYYY-MM-DD HH:MM:SS" format (rows where name is Name006 - Name010) then the following query would work :-



-- 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%'
;


resulting in :-



enter image description here






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192804%2fhow-to-get-data-of-last-6-hours-using-sqllite%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I believe that your issue is very likely that the values in the start time and end time columns are not saved in the correct format. That is for the unixepoch modifer to apply then the values MUST be in DDDDDDDDDD format.



    As per :-




    The "unixepoch" modifier (11) only works if it immediately follows a
    timestring in the DDDDDDDDDD format. This modifier causes the
    DDDDDDDDDD to be interpreted not as a Julian day number as it normally
    would be, but as Unix Time - the number of seconds since 1970. If the
    "unixepoch" modifier does not follow a timestring of the form
    DDDDDDDDDD which expresses the number of seconds since 1970 or if
    other modifiers separate the "unixepoch" modifier from prior
    DDDDDDDDDD then the behavior is undefined. For SQLite versions before
    3.16.0 (2017-01-02), the "unixepoch" modifier only works for dates between 0000-01-01 00:00:00 and 5352-11-01 10:52:47 (unix times of
    -62167219200 through 106751991167).




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



    For example consider the following (see comments) :-



    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%";


    Results



    All data :-



    enter image description here



    Your Query (it does work if the values are store in the correct format) :-



    enter image description here



    Handle data as currently stored



    Assuming that the issue is that columns start time and end time do have values store as in "YYYY-MM-DD HH:MM:SS" format (rows where name is Name006 - Name010) then the following query would work :-



    -- 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%'
    ;


    resulting in :-



    enter image description here






    share|improve this answer



























      up vote
      0
      down vote













      I believe that your issue is very likely that the values in the start time and end time columns are not saved in the correct format. That is for the unixepoch modifer to apply then the values MUST be in DDDDDDDDDD format.



      As per :-




      The "unixepoch" modifier (11) only works if it immediately follows a
      timestring in the DDDDDDDDDD format. This modifier causes the
      DDDDDDDDDD to be interpreted not as a Julian day number as it normally
      would be, but as Unix Time - the number of seconds since 1970. If the
      "unixepoch" modifier does not follow a timestring of the form
      DDDDDDDDDD which expresses the number of seconds since 1970 or if
      other modifiers separate the "unixepoch" modifier from prior
      DDDDDDDDDD then the behavior is undefined. For SQLite versions before
      3.16.0 (2017-01-02), the "unixepoch" modifier only works for dates between 0000-01-01 00:00:00 and 5352-11-01 10:52:47 (unix times of
      -62167219200 through 106751991167).




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



      For example consider the following (see comments) :-



      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%";


      Results



      All data :-



      enter image description here



      Your Query (it does work if the values are store in the correct format) :-



      enter image description here



      Handle data as currently stored



      Assuming that the issue is that columns start time and end time do have values store as in "YYYY-MM-DD HH:MM:SS" format (rows where name is Name006 - Name010) then the following query would work :-



      -- 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%'
      ;


      resulting in :-



      enter image description here






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        I believe that your issue is very likely that the values in the start time and end time columns are not saved in the correct format. That is for the unixepoch modifer to apply then the values MUST be in DDDDDDDDDD format.



        As per :-




        The "unixepoch" modifier (11) only works if it immediately follows a
        timestring in the DDDDDDDDDD format. This modifier causes the
        DDDDDDDDDD to be interpreted not as a Julian day number as it normally
        would be, but as Unix Time - the number of seconds since 1970. If the
        "unixepoch" modifier does not follow a timestring of the form
        DDDDDDDDDD which expresses the number of seconds since 1970 or if
        other modifiers separate the "unixepoch" modifier from prior
        DDDDDDDDDD then the behavior is undefined. For SQLite versions before
        3.16.0 (2017-01-02), the "unixepoch" modifier only works for dates between 0000-01-01 00:00:00 and 5352-11-01 10:52:47 (unix times of
        -62167219200 through 106751991167).




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



        For example consider the following (see comments) :-



        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%";


        Results



        All data :-



        enter image description here



        Your Query (it does work if the values are store in the correct format) :-



        enter image description here



        Handle data as currently stored



        Assuming that the issue is that columns start time and end time do have values store as in "YYYY-MM-DD HH:MM:SS" format (rows where name is Name006 - Name010) then the following query would work :-



        -- 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%'
        ;


        resulting in :-



        enter image description here






        share|improve this answer














        I believe that your issue is very likely that the values in the start time and end time columns are not saved in the correct format. That is for the unixepoch modifer to apply then the values MUST be in DDDDDDDDDD format.



        As per :-




        The "unixepoch" modifier (11) only works if it immediately follows a
        timestring in the DDDDDDDDDD format. This modifier causes the
        DDDDDDDDDD to be interpreted not as a Julian day number as it normally
        would be, but as Unix Time - the number of seconds since 1970. If the
        "unixepoch" modifier does not follow a timestring of the form
        DDDDDDDDDD which expresses the number of seconds since 1970 or if
        other modifiers separate the "unixepoch" modifier from prior
        DDDDDDDDDD then the behavior is undefined. For SQLite versions before
        3.16.0 (2017-01-02), the "unixepoch" modifier only works for dates between 0000-01-01 00:00:00 and 5352-11-01 10:52:47 (unix times of
        -62167219200 through 106751991167).




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



        For example consider the following (see comments) :-



        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%";


        Results



        All data :-



        enter image description here



        Your Query (it does work if the values are store in the correct format) :-



        enter image description here



        Handle data as currently stored



        Assuming that the issue is that columns start time and end time do have values store as in "YYYY-MM-DD HH:MM:SS" format (rows where name is Name006 - Name010) then the following query would work :-



        -- 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%'
        ;


        resulting in :-



        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 0:21

























        answered Nov 7 at 21:55









        MikeT

        13.4k102440




        13.4k102440






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192804%2fhow-to-get-data-of-last-6-hours-using-sqllite%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings