简体   繁体   中英

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; 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

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

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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM