[英]Find overlaps between timestamps in different rows in a mysql db, group by days and ID's
I have a table, where events of certain objects are listed. 我有一张表,其中列出了某些对象的事件。
There are two events: "movement" and "load". 有两个事件:“运动”和“负载”。 They can start and end, and these events are listed with timestamps when they happened.
它们可以开始和结束,并且这些事件在发生时会带有时间戳列出。
With the following request, i can get the following values: 通过以下请求,我可以获得以下值:
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)
The current version calculates these numbers for each DAY, this works fine. 当前版本会为每一天计算这些数字,效果很好。
http://sqlfiddle.com/#!2/518dd/1 http://sqlfiddle.com/#!2/518dd/1
But i will also have different ID's. 但是我也会有不同的ID。 I want now to calculate these numbers for each DAY and for each ID.
现在,我想为每个DAY和每个ID计算这些数字。 When I add this data, it crashes, see this fiddle:
当我添加此数据时,它崩溃了,请参见以下小提琴:
http://sqlfiddle.com/#!2/b43fa/6 http://sqlfiddle.com/#!2/b43fa/6
This is not a duplicate to this question: Find intersections between rows and timestamps in a mysql db , because the solution to this question is not suitable/adaptable in this case. 这不是此问题的重复项: 在mysql db中找到行和时间戳之间的交集 ,因为在这种情况下,此问题的解决方案不适合/不适用。
The problem is the query that you are starting with. 问题是您开始使用的查询。 I simply don't understand what the third column is.
我根本不明白第三列是什么。 But, the first two are much more easily calculated using:
但是,使用以下命令更容易计算前两个:
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
From this, it should be trivial to add the group by: 因此,添加组的方法很简单:
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)
As shown by Gordon, grouping is made possible by moving the filtering into the outermost SELECT
. 如Gordon所示,可以通过将过滤移到最外面的
SELECT
来进行分组。 The search for the loading events that need to enclose the movement events is done in the same way as in the earlier answer : 搜索需要包含运动事件的加载事件的方式与前面的答案相同 :
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.