HOW to SELECT data basing on both a period of date and a period of time in clickhouse

女生的网名这么多〃 提交于 2021-01-28 03:29:28

问题


I want to filter some data by both yyyymmdd(date) and hhmmss(time), but clickhouse don't support time type. So I choose datetime to combine them. But how to do such things:

This is code of dolphindb(which supports second type to represent hhmmss.

select avg(ofr + bid) / 2.0 as avg_price
from taq
where
    date between 2007.08.05 : 2007.08.07,
    time between 09:30:00 : 16:00:00
group by symbol, date

This is code of clickhouse, but a logical problematic code.

SELECT avg(ofr + bid) / 2.0 AS avg_price
FROM taq
WHERE
    time BETWEEN '2007-08-05 09:30:00' AND '2007-08-07 16:00:00'
GROUP BY symbol, toYYYYMMDD(time)
;

how to express it in sql just like the dolphindb code?


回答1:


Assume that you just want to average the trading price in normal trading hours, excluding after hour trading, then a possible solution:

SELECT avg(ofr + bid) / 2.0 AS avg_price
FROM taq
WHERE
    toYYYYMMDD(time) BETWEEN 20070805 AND 20070807 AND
    toYYYYMMDDhhmmss(time)%1000000 BETWEEN 93000 and 160000
GROUP BY symbol, toYYYYMMDD(time)

This filters the taq table within specified date and time.



来源:https://stackoverflow.com/questions/55788679/how-to-select-data-basing-on-both-a-period-of-date-and-a-period-of-time-in-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!