Sql-server – Aggregate interval data to hourly

countsql server

I am getting data for 15 minutes interval. I would like to aggregate this data to hourly.Can anybody advise how should i do this query to sum the interval values and display it?

enter image description here

Best Answer

You can do a simple GROUP BY with DATEPART applied to the time column. I'm assuming that you'll always get a row every 15 minutes and you don't need to worry about gaps in the data.

SELECT 
  code
, date_column AS [date]
, DATEPART(HOUR, time_column) AS hourly
, SUM(value) AS value
FROM test_table
GROUP BY
  code
, date_column
, DATEPART(HOUR, time_column);

The following references may be useful to you: