您好我正在尝试将间隔分组30分钟到最近的小时。我可以分组30分钟,但我无法进行最近的小时逻辑工作。我附上了一张快照,您可以从中了解我想要的内容。
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)
答案 0 :(得分:0)
也许你不应该只选择timestamp
,而是选择你所分组的值(UNIX_TIMESTAMP(timestamp) div 1800
),只有你还应该将它转换回可读的日期时间值:
from_unixtime((UNIX_TIMESTAMP(timestamp) div 1800)*1800)
所以查询将是这样的:
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)