Skip to content
Advertisement

Aggregate time from 15-minute interval to single hour in SQL

I have below table structure in SQL Server:

StartDate   Start       End         Sales
==============================================
2020-08-25  00:00:00    00:15:00    291.4200
2020-08-25  00:15:00    00:30:00    401.1700
2020-08-25  00:30:00    00:45:00    308.3300
2020-08-25  00:45:00    01:00:00    518.3200
2020-08-25  01:00:00    01:15:00    247.3700
2020-08-25  01:15:00    01:30:00    115.4700
2020-08-25  01:30:00    01:45:00    342.3800
2020-08-25  01:45:00    02:00:00    233.0900
2020-08-25  02:00:00    02:15:00    303.3400
2020-08-25  02:15:00    02:30:00    11.9000
2020-08-25  02:30:00    02:45:00    115.2400
2020-08-25  02:45:00    03:00:00    199.5200
2020-08-25  06:00:00    06:15:00    0.0000
2020-08-25  06:15:00    06:30:00    45.2400
2020-08-25  06:30:00    06:45:00    30.4800
2020-08-25  06:45:00    07:00:00    0.0000
2020-08-25  07:00:00    07:15:00    0.0000
2020-08-25  07:15:00    07:30:00    69.2800

Is there a way to group above data into one hour interval instead of 15 minute interval? It has to be based on start and end columns.

Thanks,

Advertisement

Answer

Maybe something like the following using datepart?

select startdate, DatePart(hour,start) [Hour], Sum(sales) SalesPerHour
from t
group by startdate,  DatePart(hour,start)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement