[英]MYSQL - Determining previous values in a row
I'm looking to determine the previous 2 values in a row after grouping the timestamp into 3 min groups; 在将时间戳分组为3分钟后,我希望连续确定前两个值; For example using the table below
例如使用下表
Timestamp Total
2013-10-11 00:01:00 1
2013-10-11 00:02:00 5
2013-10-11 00:03:00 6
2013-10-11 00:04:00 3
2013-10-11 00:05:00 9
2013-10-11 00:06:00 10
2013-10-11 00:07:00 12
2013-10-11 00:08:00 10
2013-10-11 00:09:00 15
2013-10-11 00:10:00 12
. .
. .
The code I have to group the timestamp into 3 minutes groups and display the max total for each 3min group is 我必须将时间戳分组为3分钟的组,并显示每个3min组的最大总计为
SELECT
max (Timestamp)
AS tstamp
max(total)
FROM table
Group by ROUND(UNIX_TimeStamp(timestamp)/180)
The output given is 给定的输出是
tstamp max( Total)
2013-10-11 00:01:00 1
2013-10-11 00:04:00 6
2013-10-11 00:07:00 12
2013-10-11 00:10:00 15
.
I'm looking to add the previous 2 values in each 3 min group were the max value is found so I get 我正在寻找在每个3分钟组中添加之前的2个值,以便找到最大值,因此我得到了
tstamp max( Total) Previous1 Previous2
2013-10-11 00:01:00 1 0 0
2013-10-11 00:04:00 6 5 1
2013-10-11 00:07:00 12 10 9
2013-10-11 00:10:00 15 10 12
.
I'm thinking along the lines that one way would be to use max(timestamp) - INTERVAL 1 MINUTE and then max(timestamp) -INTERVAL 2 MINUTE in a nested select. 我一直认为,一种方法是在嵌套选择中使用max(timestamp)-INTERVAL 1 MINUTE,然后再使用max(timestamp)-INTERVAL 2 MINUTE。 Something along those lines but I am not entirely sure on the approach
遵循这些原则,但我不确定该方法
Is there a simpler/cleaner way to approach this, is the question? 有没有更简单/更清洁的方法来解决这个问题?
So I started with what you had: 所以我从你拥有的开始:
select
max(`Timestamp`) AS 'Timestamp'
,max(total) as total
, ROUND(UNIX_TimeStamp(`Timestamp`)/180) as roundBy3
FROM `time`
Group by ROUND(UNIX_TimeStamp(`Timestamp`)/180)
) summary
Then I added some more detail: 然后,我添加了更多详细信息:
select `Timestamp`
, total
, ROUND(UNIX_TimeStamp(Timestamp)/180) as roundBy3
, @lastT3 := if(@lastT3 is null, 0, @lastT2) as saveForNext3
, @lastT2 := if(@lastT2 is null, 0, @lastT1) as saveForNext2
, @lastT1 := if(@lastT1 is null, 0, total) as saveForNext1
from `time`
, ( select @lastT1 := null
, @lastT2 := null
, @lastT3 := null ) sqlvars
order by roundBy3 asc, total ASC
And finally I rolled it all together: 最后,我将所有内容汇总在一起:
select detail.Timestamp
, detail.total
, detail.saveForNext3
, detail.saveForNext2
from (
select
max(`Timestamp`) AS 'Timestamp'
,max(total) as total
, ROUND(UNIX_TimeStamp(`Timestamp`)/180) as roundBy3
FROM `time`
Group by ROUND(UNIX_TimeStamp(`Timestamp`)/180)
) summary
join (
select `Timestamp`
, total
, ROUND(UNIX_TimeStamp(Timestamp)/180) as roundBy3
, @lastT3 := if(@lastT3 is null, 0, @lastT2) as saveForNext3
, @lastT2 := if(@lastT2 is null, 0, @lastT1) as saveForNext2
, @lastT1 := if(@lastT1 is null, 0, total) as saveForNext1
from `time`
, ( select @lastT1 := null
, @lastT2 := null
, @lastT3 := null ) sqlvars
order by roundBy3 asc, total ASC
) detail
on summary.roundBy3 = detail.roundBy3 and summary.total = detail.total
group by ROUND(UNIX_TimeStamp(summary.Timestamp)/180);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.