Skip to content
Advertisement

Select max data in interval of 1 hour within a time range in sql

I have some difficulties with sql server to select max value in a column in interval of 1 hour within a time range.

My table look like this:

How do I select max Value column in interval of 1 hour within a time range from 2019-04-21 08:21:50 to 2019-04-22 11:47:50

I want my output to be like this:

Advertisement

Answer

You can use ROW_NUMBER() in a subquery to assign a rank to each record withing groups of each records having the same timestamp (minutes and seconds excluded) ordered by value, and then filter on the top record in each group in the outer query:

Demo on DB Fiddle:

     id | value | timestamp          
------: | :---- | :------------------
2030105 | 32.20 | 2019-04-21 08:21:50
2030201 | 32.20 | 2019-04-21 09:50:30
2030202 | 32.00 | 2019-04-22 10:19:50
2030205 | 31.20 | 2019-04-22 11:47:50

If you want the average value within each 1h range as well, then:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement