简体   繁体   中英

Grouping into interval of 30 minutes to its nearest hour

Hello I am trying to group interval by 30 mins to the nearest hour. I am able to group by 30 mins but I am unable to make nearest hour logic work. I have attached a snapshot from which you can get an idea of what I want.

  select SUBSTRING_INDEX(GROUP_CONCAT(CAST(price AS CHAR) ORDER BY `timestamp`), ',', 1 ) as open,
                    max(price) high,
                    min(price) low,
                    SUBSTRING_INDEX(GROUP_CONCAT(CAST(price AS CHAR) ORDER BY `timestamp` desc), ',', 1 ) as close,
                    coinrace.watch_list.symbol,
                    timestamp
                    from coinrace.watch_quote
                    join coinrace.watch_list on coinrace.watch_list.watch_id = coinrace.watch_quote.watch_id
                    where (`timestamp` between '2015-12-03' and '2015-12-10')
                    and coinrace.watch_quote.serial_number = 1
                    and coinrace.watch_quote.BuyOrSell='buy'
                    and coinrace.watch_list.symbol='MCOEUR'
                    group by  UNIX_TIMESTAMP(timestamp) div (30*60)

请检查一下

Perhaps you should select not just timestamp , but the value you are grouping by ( UNIX_TIMESTAMP(timestamp) div 1800 ), only you should also convert it back to a readable datetime value:

from_unixtime((UNIX_TIMESTAMP(timestamp) div 1800)*1800)

So the query would be like this:

  select SUBSTRING_INDEX(GROUP_CONCAT(CAST(price AS CHAR) ORDER BY `timestamp`), ',', 1 ) as open,
                    max(price) high,
                    min(price) low,
                    SUBSTRING_INDEX(GROUP_CONCAT(CAST(price AS CHAR) ORDER BY `timestamp` desc), ',', 1 ) as close,
                    coinrace.watch_list.symbol,
                    from_unixtime((UNIX_TIMESTAMP(timestamp) div 1800)*1800) as timestamp
                    from coinrace.watch_quote
                    join coinrace.watch_list on coinrace.watch_list.watch_id = coinrace.watch_quote.watch_id
                    where (`timestamp` between '2015-12-03' and '2015-12-10')
                    and coinrace.watch_quote.serial_number = 1
                    and coinrace.watch_quote.BuyOrSell='buy'
                    and coinrace.watch_list.symbol='MCOEUR'
                    group by  UNIX_TIMESTAMP(timestamp) div (30*60)

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