SQLLite time entry and time exit from eventime

后端 未结 2 1397
攒了一身酷
攒了一身酷 2021-01-29 09:42

I have two tables, DATA and EVENTS, with the following data:

EVENTS
EventIndex  ObjID   LocID   EventData   EventTime       EventType
8         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-29 10:02

    Here is a quick SQL dump for working on the table.

    CREATE TABLE EVENTS (EventIndex int, objID int, eventtime datetime);
    INSERT INTO "EVENTS" VALUES(83707365,3519434,'2013-05-19 11:32:11');
    INSERT INTO "EVENTS" VALUES(83707849,3519434,'2013-05-19 11:35:18');
    INSERT INTO "EVENTS" VALUES(83714233,888799,'2013-05-19 12:24:25');
    INSERT INTO "EVENTS" VALUES(83714233,888799,'2013-05-19 12:32:18');
    CREATE TABLE DATA (eventindex int, tagname char, tagvalue char);
    INSERT INTO "DATA" VALUES(83714233,'ObjName','Peter');
    INSERT INTO "DATA" VALUES(83714233,'LocName','H118');
    INSERT INTO "DATA" VALUES(83715200,'ObjName','Peter');
    INSERT INTO "DATA" VALUES(83715200,'LocName','H118');
    INSERT INTO "DATA" VALUES(83707365,'ObjName','John');
    INSERT INTO "DATA" VALUES(83707849,'ObjName','John');
    INSERT INTO "DATA" VALUES(83707365,'LocName','H118');
    INSERT INTO "DATA" VALUES(83707849,'LocName','H118');
    

    Step 1, get the maximal and minimal eventtype for each ObjID:

    SELECT ObjID, Min(EventIndex) as EventIndex, Min(EventTime) as Entry, Max(EventTime) as Exit
      FROM EVENTS GROUP BY ObjID;
    
    
    ObjID    EventIndex     Entry      Exit
    3519434   83707365   12:24:45  12:32:18
     888799   83714233   11:32:11  11:25:18
    

    Now this reveals some problems with your data structure. For each ObjID, you have two EventIndex, so which one should you pick? Here, I have simply selected the first (smallest) of the two.

    Step 2, because sqlite3 dislikes performing operations on aggregates, we make it into a view:

    CREATE VIEW single_events AS 
    SELECT ObjID, Min(EventIndex) as EventIndex, Min(EventTime) as Entry, Max(EventTime) as Exit FROM EVENTS GROUP BY ObjID;
    

    Step 3, get the total time by operating on the view:

    SELECT *, time(strftime('%s', exit) - strftime('%s', entry), 'unixepoch') as total 
      FROM single_events;
    ObjID       EventIndex  Entry                Exit                 total
    ----------  ----------  -------------------  -------------------  ----------
    888799      83714233    2013-05-19 12:24:25  2013-05-19 12:32:18  00:07:53
    3519434     83707365    2013-05-19 11:32:11  2013-05-19 11:35:18  00:03:07
    

    Step 4, merge this with DATA.

    SELECT d1.TagValue as Name, d2.TagValue as Location, Entry, Exit, time(strftime('%s', exit) - strftime('%s', entry), 'unixepoch') as total
      FROM single_events
      LEFT JOIN DATA as d1 USING (EventIndex)
      LEFT JOIN DATA as d2 USING (EventIndex)
      WHERE d1.tagname = 'ObjName' AND d2.tagname = 'LocName';
    

    Of course, if it is a very large data set, you might benefit from pouring d1 and d2 into a temporary table before running step 4.

提交回复
热议问题