Suppose SQL Server is below. If this logic is most likely not applicable to your RDBMS, but probably another function is to get the time part from the date and time, and the BETWEEN behavior may be different (this is a comprehensive range in SQL Server).
SELECT CAST([Date] AS Date) AS [Date], COUNT(CASE WHEN DATEPART(hour, [Date]) BETWEEN 9 AND 11 THEN 1 ELSE NULL END) AS [Sales 9am-12pm], COUNT(CASE WHEN DATEPART(hour, [Date]) BETWEEN 12 AND 14 THEN 1 ELSE NULL END) AS [Sales 12pm-3pm], COUNT(CASE WHEN DATEPART(hour, [Date]) BETWEEN 18 AND 20 THEN 1 ELSE NULL END) AS [Sales 6pm-9pm] FROM Table GROUP BY CAST([Date] AS Date)
Note. Previous versions of SQL Server require a few more hoops to get only a date from a date and time. e.g. CAST(FLOOR( CAST( GETDATE() AS FLOAT ) ) AS DATETIME (From here )
source share