从行而不是列中减去时间查询不起作用

时间:2013-11-30 17:46:29

标签: mysql

我有一个twext查询,它可以查看两个ROWS之间的时间差异 - 而不是列。 每行都有一个记录号并且是顺序的 每行都有一个活动代码,用于指示哪个关闭以及哪个关闭 下面是我开发的用于尝试设置以查找时间差异的查询。 但是我仍然无法减去行之间的时间(这是必需的)

SELECT g.groupId,g.name AS groupName,l.logId,l.name AS logName, l.ordering AS logOrder,
           g1.parentId AS parentId1, g1.name AS group1Name,g2.parentId AS parentId2, g2.name AS group2Name,
          g3.parentId AS parentId3, g3.name AS group3Name,a.activityId AS activityId,a.ordering AS activityOrder,
        a.name AS activityName,l1.recordId,l1.started AS timeON, l1.completed ,l1.userId,l1.note, u.name,
        NULL AS timeOFF
      FROM logs l 
          INNER JOIN groups g ON g.groupId = l.groupId
          LEFT JOIN groups g1 ON g.parentId = g1.groupId
          LEFT JOIN groups g2 ON g1.parentId = g2.groupId
          LEFT JOIN groups g3 ON g2.parentId = g3.groupId                
          INNER JOIN activities a ON l.logId = a.logId AND a.activityId = 1257
         INNER JOIN log1644 l1 ON a.activityId = l1.activityId 
          INNER JOIN users u ON l1.userId = u.userId
          WHERE  l.logID = 1644


        UNION

        SELECT g.groupId,g.name AS groupName,l.logId,l.name AS logName, l.ordering AS logOrder,
           g1.parentId AS parentId1, g1.name AS group1Name,g2.parentId AS parentId2, g2.name AS group2Name,
          g3.parentId AS parentId3, g3.name AS group3Name,a.activityId AS activityId,a.ordering AS activityOrder,
        a.name AS activityName,l1.recordId,NULL AS timeOn,l1.completed,l1.userId,l1.note, u.name,
       l1.started AS timeOFF
      FROM logs l 
          INNER JOIN groups g ON g.groupId = l.groupId
          LEFT JOIN groups g1 ON g.parentId = g1.groupId
          LEFT JOIN groups g2 ON g1.parentId = g2.groupId
          LEFT JOIN groups g3 ON g2.parentId = g3.groupId                
          INNER JOIN activities a ON l.logId = a.logId  AND a.activityId = 1291
         Inner JOIN log1644 l1 ON a.activityId = l1.activityId 
          INNER JOIN users u ON l1.userId = u.userId
          WHERE  l.logID = 1644
          ORDER BY recordId

表格在执行时看起来如此。

194 Building Status 1644    Free Cooling Tracker    0   (null)  (null)  (null)      (null)  (null)  (null)  1257    0   Enable Free Cooling Mode    1   11/14/2013 11:06:29 PM  11/14/2013 11:31:05 PM  34      David Akers (null)
194 Building Status 1644    Free Cooling Tracker    0   (null)  (null)  (null)  (null)  (null)  (null)  1291    1   Disable Free Cooling Mode   2   (null)  11/15/2013 2:12:19 PM   40      Steve Young 11/15/2013 2:09:57 PM
194 Building Status 1644    Free Cooling Tracker    0   (null)  (null)  (null)  (null)  (null)  (null)  1257    0   Enable Free Cooling Mode    3   11/18/2013 4:26:34 AM   11/18/2013 5:54:29 AM   34      David Akers (null)
194 Building Status 1644    Free Cooling Tracker    0   (null)  (null)  (null)  (null)  (null)  (null)  1291    1   Disable Free Cooling Mode   4   (null)  11/18/2013 4:49:37 PM   32      Patrick Willis  11/18/2013 4:48:53 PM
194 Building Status 1644    Free Cooling Tracker    0   (null)  (null)  (null)  (null)  (null)  (null)  1257    0   Enable Free Cooling Mode    5   11/19/2013 2:54:52 AM   11/19/2013 3:17:03 AM   34      David Akers (null)
194 Building Status 1644    Free Cooling Tracker    0   (null)  (null)  (null)  (null)  (null)  (null)  1291    1   Disable Free Cooling Mode   6   (null)  11/19/2013 3:43:52 PM   32      Patrick Willis  11/19/2013 3:42:00 PM
194 Building Status 1644    Free Cooling Tracker    0   (null)  (null)  (null)  (null)  (null)  (null)  1257    0   Enable Free Cooling Mode    7   11/21/2013 2:24:38 AM   11/21/2013 2:58:55 AM   34      David Akers (null)
194 Building Status 1644    Free Cooling Tracker    0   (null)  (null)  (null)  (null)  (null)  (null)  1291    1   Disable Free Cooling Mode   8   (null)  11/21/2013 2:27:23 PM   32      Patrick Willis  11/21/2013 2:08:06 PM

0 个答案:

没有答案