简体   繁体   中英

how to count data occurrence between certain DateTime range in mysql

I am sorry for this kind of newbie question, but it is quite hard for me. My MySQL table holds high-frequency stock prices. On each trading date, data starts from day 1 18:00 to day 2 17:00 . I want to count the data occurrence for each trading day between the above trading hour. I know how to count for a single day just as following code

SELECT COUNT(*) FROM table WHERE date BETWEEN '2021-08-25 18:00:00' AND '2021-08-26 17:00:00' 

But how to count all the days in the table?

My table looks like this: 在此处输入图像描述

What I want to looks like this: 在此处输入图像描述

Thanks in advance.

Appreciate that your "day" starts at 6pm. So, we can normalize each datetime by subtracting 18 hours, and then aggregate:

SELECT DATE(date - INTERVAL 18 HOUR), COUNT(*) AS cnt
FROM yourTable
GROUP BY 1;

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