简体   繁体   English

分组到30分钟的间隔到最近的小时

[英]Grouping into interval of 30 minutes to its nearest hour

Hello I am trying to group interval by 30 mins to the nearest hour. 你好我想把间隔时间缩短30分钟到最近的一小时。 I am able to group by 30 mins but I am unable to make nearest hour logic work. 我可以分组30分钟,但我无法进行最近的小时逻辑工作。 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: 也许您应该不仅选择timestamp ,而是选择您要分组的值( UNIX_TIMESTAMP(timestamp) div 1800 ),只有您还应该将其转换回可读的日期时间值:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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