SQLite时间输入和时间从eventim退出

时间:2013-05-23 11:47:25

标签: sqlite

我有两个表DATAEVENTS,其中包含以下数据:

EVENTS
EventIndex  ObjID   LocID   EventData   EventTime       EventType
83707365    3519434 10376   0           2013-05-19 11:32:11 137
83707849    3519434 10374   0           2013-05-19 11:35:18 137 
83714233    888799  10376   0           2013-05-19 12:24:45 137 
83715200    888799  10184   0           2013-05-19 12:32:18 137

DATA
EventIndex  TagName TagValue    
83714233    ObjName Peter
83714233    LocName H118
83715200    ObjName Peter
83715200    LocName H116
83707365    ObjName John
83707365    LocName H118
83707849    ObjName John
83707849    LocName H116

从哪里开始SQL SQLLite查询?

我想得到结果:

Name   Location  Entry      Exit        Total
Peter  H118      12:24:45   12:32:18    00:07:33
John   H118      11:32:11   11:35:18    00:03:07

嗨meewoK,可以给出以下案例:

名称位置输入退出总计
彼得H118 12:24:45 12:32:18 00:07:33
John H118 11:32:11 11:35:18 00:03:07
约翰H118 11:44:52 11:45:27 00:00:35
约翰H118 12:31:15 12:32:46 00:01:31

而我正在尝试修改您的查询,但我找不到解决方案。感谢

此代码适用于SQL SERVER:

SELECT E.EventIndex,N.tagvalue AS Name,L.tagvalue AS Location,E.eventtime AS Entry,NEV。[Exit]

    [事件] E
    INNER JOIN [DATA] N ON E.EventIndex = N.eventindex AND N.tagname ='ObjName'
    INNER JOIN [DATA] L ON E.EventIndex = L.eventindex AND L.tagname ='LocName'
    外部申请(
        SELECT TOP(1)NE.eventtime AS [退出]
        从
            [事件] NE
            INNER JOIN [DATA] NL ON NE.EventIndex = NL.eventindex AND NL.tagname ='ObjName'
        WHERE
            NE.EventIndex> E.EventIndex
            AND NL.tagvalue = N.tagvalue
        订购单             NE.EventIndex

) NEV  

何处L.tagvalue ='H118'

有人可以帮我把它传递给SQLite吗? 感谢

2 个答案:

答案 0 :(得分:0)

这是一个用于处理表的快速SQL转储。

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

第1步,获取每个eventtype的最大和最小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

现在,这揭示了数据结构的一些问题。对于每个ObjID,您有两个EventIndex,那么您应该选择哪一个?{1}}在这里,我只选择了两者中的第一个(最小的)。

第2步,因为sqlite3不喜欢对聚合执行操作,我们将其变为视图:

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

步骤3,通过操作视图获取总时间:

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

步骤4,将其与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';

当然,如果它是一个非常大的数据集,您可能会在运行第4步之前将d1d2放入临时表中。

答案 1 :(得分:0)

这样的事情可以让你很好地开始:

SQL小提琴:http://www.sqlfiddle.com/#!2/6e6a7/33

SELECT d.TagValue AS Name,
       d2.TagValue AS LOCATION,
       e.eventTime AS Entry,

  ( SELECT eventtime
   FROM EVENTS e2
   WHERE e2.objID = e.objID
     AND e2.eventTime > e.eventTime LIMIT 1) AS ExitTime,
       (e.eventTime -
          ( SELECT eventtime
           FROM EVENTS e2
           WHERE e2.objID = e.objID
             AND e2.eventTime > e.eventTime LIMIT 1)) AS TotalTime
FROM events e,
     DATA d,
          DATA d2
WHERE e.eventIndex = d.eventIndex
  AND e.eventIndex = d2.eventIndex
  AND d.TagName = "ObjName"
  AND d2.TagName = "LocName"
  AND mod(
            ( SELECT count(*)
             FROM EVENTS e2
             WHERE e2.objID = e.objID
               AND e2.eventTime < e.eventTime),2) = 0
ORDER BY EventTime;

结果:

NAME    LOCATION    ENTRY                   EXITTIME                  TOTALTIME
John    H118    May, 19 2013 11:32:11+0000  May, 19 2013 11:35:18+0000  -307
Peter   H118    May, 19 2013 12:24:45+0000  May, 19 2013 12:32:18+0000  -773

我添加了额外的事件,以证明退出一个被视为另一个事件的事件。

如果我们说进入和退出不能在不同日期发生,则可以优化上述查询,例如USER进入11.59PM并离开1.00AM ...

SELECT d.TagValue AS Name,
       d2.TagValue AS LOCATION,
       e.eventTime AS Entry,

  ( SELECT eventtime
   FROM EVENTS e2
   WHERE e2.objID = e.objID
     AND e2.eventTime > e.eventTime LIMIT 1) AS ExitTime,
       (e.eventTime -
          ( SELECT eventtime
           FROM EVENTS e2
           WHERE e2.objID = e.objID
             AND e2.eventTime > e.eventTime AND
             DATE(e2.eventTime) = DATE(e.eventTime) LIMIT 1)) AS TotalTime
FROM events e,
     DATA d,
          DATA d2
WHERE e.eventIndex = d.eventIndex
  AND e.eventIndex = d2.eventIndex
  AND d.TagName = "ObjName"
  AND d2.TagName = "LocName"
  AND mod(
            ( SELECT count(*)
             FROM EVENTS e2
             WHERE e2.objID = e.objID
               AND e2.eventTime < e.eventTime AND
               DATE(e2.eventTime) = DATE(e.eventTime)),2) = 0
ORDER BY EventTime;

SQL小提琴:http://www.sqlfiddle.com/#!2/6e6a7/35

这也假设每个entry必须有一个exit,这就是我使用模运算符的原因。 Even的所有Object个活动都是一个条目,所有Odd个活动都是exit

对于更多条件,显然必须修改查询。

更新:使用SQL Lite(无mod但%)

SQLFiddle:http://www.sqlfiddle.com/#!7/6e6a7/4

SELECT d.TagValue AS Name,
       d2.TagValue AS LOCATION,
       e.eventTime AS Entry,

  ( SELECT eventtime
   FROM EVENTS e2
   WHERE e2.objID = e.objID
     AND e2.eventTime > e.eventTime LIMIT 1) AS ExitTime,
       (e.eventTime -
          ( SELECT eventtime
           FROM EVENTS e2
           WHERE e2.objID = e.objID
             AND e2.eventTime > e.eventTime LIMIT 1)) AS TotalTime
FROM events e,
     DATA d,
          DATA d2
WHERE e.eventIndex = d.eventIndex
  AND e.eventIndex = d2.eventIndex
  AND d.TagName = "ObjName"
  AND d2.TagName = "LocName"
  AND (
            ( SELECT count(*)
             FROM EVENTS e2
             WHERE e2.objID = e.objID
               AND e2.eventTime < e.eventTime)%2) = 0
ORDER BY EventTime;