Grouping Data by Time Intervals in SQL Server: Hourly and 10-Minute Aggregations

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

YearMonthDayHourEventCount
202422382
202422392
2024223102
2024223111

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

YearMonthDayHourMinuteBlockEventCount
20242238101
20242238401
2024223901
20242239101
202422310301
202422310401
20242231101

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.

Related Posts

Troubleshooting Missing SQL Server Statistics

Learn how to diagnose and fix missing SQL Server statistics through a practical troubleshooting guide, including step-by-step solutions and best practices.

Read more

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from The DBA Hub

Subscribe now to keep reading and get access to the full archive.

Continue reading