-1

我有两个表,DATA并且EVENTS,具有以下数据:

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,可以给出以下情况:

名称 位置 入口 出口 总计
Peter H118 12:24:45 12:32:18 00:07:33
John H118 11:32:11 11:35:18 00:03:07
John 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]
FROM
[EVENTS] 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 [Exit]
FROM
[EVENTS] NE
INNER JOIN [数据] NL ON NE.EventIndex = NL.eventindex AND NL.tagname = 'ObjName'
WHERE
NE.EventIndex > E.EventIndex
AND NL.tagvalue = N.tagvalue
ORDER BY
NE.EventIndex

) NEV  

WHERE L.tagvalue = 'H118'

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

4

2 回答 2

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,这就是我使用模运算符的原因。a 的所有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;
于 2013-05-23T12:58:03.510 回答
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');

eventtype第 1 步,获取每个的最大值和最小值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,那么你应该选择哪一个?在这里,我只是选择了两者中的第一个(最小的)。

第二步,因为 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;

第三步,对视图进行操作得到总时间:

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 步之前将d1其倒入d2临时表中。

于 2013-05-23T12:25:42.763 回答