0

我有一张表,其中列出了某些对象的事件。

有两个事件:“移动”和“加载”。它们可以开始和结束,并且这些事件会在它们发生时用时间戳列出。

通过以下请求,我可以获得以下值:

sum of time, when movement took place (value in the fiddle: 421)
sum of time, when load took place (value in the fiddle: 520)
sum of time, when movement and load took place (value in the fiddle: 391)

当前版本为每一天计算这些数字,这很好用。

http://sqlfiddle.com/#!2/518dd/1

但我也会有不同的ID。我现在想为每个 DAY 和每个 ID 计算这些数字。当我添加这些数据时,它会崩溃,请看这个小提琴:

http://sqlfiddle.com/#!2/b43fa/6

这不是这个问题的重复:Find intersections between rows and timestamps in a mysql db,因为这个问题的解决方案在这种情况下不适合/不适用。

4

2 回答 2

2

问题是您开始的查询。我根本不明白第三列是什么。但是,前两个更容易计算使用:

select (sum(case when event = 'movement end' then timestamp end) -
        sum(case when event = 'movement start' then timestamp end)
       ),
       (sum(case when event = 'load end' then timestamp end) -
        sum(case when event = 'load start' then timestamp end)
       )
from table1

由此,通过以下方式添加组应该是微不足道的:

select id, date(timestamp),
        (sum(case when event = 'movement end' then timestamp end) -
        sum(case when event = 'movement start' then timestamp end)
       ),
       (sum(case when event = 'load end' then timestamp end) -
        sum(case when event = 'load start' then timestamp end)
       )
from table1
group by id, date(timestamp)
于 2013-02-27T14:49:15.603 回答
2

正如 Gordon 所示,通过将过滤器移到最外层,可以进行分组SELECT。搜索需要包含移动事件的加载事件的方法与前面的答案相同:

SELECT id,
       date(timestamp, 'unixepoch') AS date,
       (SUM(CASE WHEN event = 'movement end'   THEN timestamp END) -
        SUM(CASE WHEN event = 'movement start' THEN timestamp END)
       ) AS all_movement,
       (SUM(CASE WHEN event = 'load end'   THEN timestamp END) -
        SUM(CASE WHEN event = 'load start' THEN timestamp END)
       ) AS all_load,
       (SUM(CASE WHEN event = 'movement end' AND
                      (SELECT event
                       FROM Table1 b
                       WHERE timestamp = (SELECT MIN(timestamp)
                                          FROM Table1 c
                                          WHERE c.timestamp >= a.timestamp
                                            AND c.id = a.id
                                            AND c.event LIKE 'load %')
                         AND b.id = a.id
                         AND b.event LIKE 'load %'
                      ) = 'load end'
            THEN timestamp END) -
        SUM(CASE WHEN event = 'movement start' AND
                      (SELECT event
                       FROM Table1 b
                       WHERE timestamp = (SELECT MAX(timestamp)
                                          FROM Table1 c
                                          WHERE c.timestamp <= a.timestamp
                                            AND c.id = a.id
                                            AND c.event LIKE 'load %')
                         AND b.id = a.id
                         AND b.event LIKE 'load %'
                      ) = 'load start'
            THEN timestamp END)
       ) AS load_movement
FROM Table1 a
GROUP BY id,
         date(timestamp, 'unixepoch')
于 2013-02-27T21:15:31.163 回答