In SQL Server, grouping data by time intervals such as by hour or by 10 minutes requires manipulation of the date and time values so that rows falling within each interval are grouped together. This can be achieved using the DATEPART function for hourly grouping or a combination of DATEPART and arithmetic operations for more granular groupings like every 10 minutes. Here’s how you can do it:
Example:
Let’s first create a sample table and populate it with some datetime values. We’ll use this table to demonstrate how to group by these intervals.
Step 1: Create a Sample Table
Let’s create a table named SampleDateTable:
CREATE TABLE SampleDateTable (
ID INT IDENTITY(1,1) PRIMARY KEY,
EventDateTime DATETIME
);
Step 2: Insert Sample Data
Next, insert some sample datetime values into SampleDateTable:
INSERT INTO SampleDateTable (EventDateTime) VALUES
('2024-02-23 08:15:00'),
('2024-02-23 08:45:00'),
('2024-02-23 09:05:00'),
('2024-02-23 09:15:00'),
('2024-02-23 10:30:00'),
('2024-02-23 10:40:00'),
('2024-02-23 11:00:00');
Step 3: Group by Hour
To group the results by hour, you can use the DATEPART function to extract the hour part from the EventDateTime column:
SELECT
DATEPART(YEAR, EventDateTime) AS Year,
DATEPART(MONTH, EventDateTime) AS Month,
DATEPART(DAY, EventDateTime) AS Day,
DATEPART(HOUR, EventDateTime) AS Hour,
COUNT(*) AS EventCount
FROM SampleDateTable
GROUP BY
DATEPART(YEAR, EventDateTime),
DATEPART(MONTH, EventDateTime),
DATEPART(DAY, EventDateTime),
DATEPART(HOUR, EventDateTime)
ORDER BY Year, Month, Day, Hour;
This query groups the events by year, month, day, and hour, then counts the number of events for each hour.
Group by Hour Results
| Year | Month | Day | Hour | EventCount |
|---|---|---|---|---|
| 2024 | 2 | 23 | 8 | 2 |
| 2024 | 2 | 23 | 9 | 2 |
| 2024 | 2 | 23 | 10 | 2 |
| 2024 | 2 | 23 | 11 | 1 |
Step 4: Group by 10 Minutes
To group by 10-minute intervals, you can use a combination of DATEPART and some arithmetic to round the minutes down to the nearest 10-minute mark:
SELECT
DATEPART(YEAR, EventDateTime) AS Year,
DATEPART(MONTH, EventDateTime) AS Month,
DATEPART(DAY, EventDateTime) AS Day,
DATEPART(HOUR, EventDateTime) AS Hour,
(DATEPART(MINUTE, EventDateTime) / 10) * 10 AS MinuteBlock,
COUNT(*) AS EventCount
FROM SampleDateTable
GROUP BY
DATEPART(YEAR, EventDateTime),
DATEPART(MONTH, EventDateTime),
DATEPART(DAY, EventDateTime),
DATEPART(HOUR, EventDateTime),
(DATEPART(MINUTE, EventDateTime) / 10) * 10
ORDER BY Year, Month, Day, Hour, MinuteBlock;
Group by 10 Minutes Results
| Year | Month | Day | Hour | MinuteBlock | EventCount |
|---|---|---|---|---|---|
| 2024 | 2 | 23 | 8 | 10 | 1 |
| 2024 | 2 | 23 | 8 | 40 | 1 |
| 2024 | 2 | 23 | 9 | 0 | 1 |
| 2024 | 2 | 23 | 9 | 10 | 1 |
| 2024 | 2 | 23 | 10 | 30 | 1 |
| 2024 | 2 | 23 | 10 | 40 | 1 |
| 2024 | 2 | 23 | 11 | 0 | 1 |
These results demonstrate how the data is grouped by each hour and by each 10-minute interval, with the count of events for each grouping.
This query rounds the minutes down to the nearest 10-minute mark (for example, 15 becomes 10, 25 becomes 20) and groups the events accordingly, providing a count of events for each 10-minute interval.
These examples demonstrate how to group datetime values by hour and by 10-minute intervals in SQL Server, which can be particularly useful for analyzing time-based patterns in your data.