I have a table with below structure:
id INT, order_id INT, datex DATE, timex TIME
For example I want to select count of order ID's from 2017-10-24
to 2017-10-26
in specific periods of time. Like 2:0:0 - 4:0:0
,4:0:0 - 6:0:0
etc and make a result like below:
period | ordersID's
2:0:0 - 4:0:0 | 5
4:0:0 - 6:0:0 | 8
Can I do this by just a query or I should use other ways?
First thing is your time periods are overlapping. It is not clear that if orders occur at 4 what period they should belong in. For my examples below I am assuming orders occurring on the hour at the end of the period belong to the next period.
One way to solve this with just a query is to use a case statement in your SELECT and GROUP BY clauses.
Another way to solve this is to create a table with your time periods in it and join it. I prefer this solution because it is easier to maintain.