Ramesh Kotha
Ramesh Kotha

Reputation: 8322

Average data for every four hours in sql?

i have three fields in database day, hour, value every hour value will be stored in the database. now i would like to take every four hours average value. for one day i need to get 6 rows with each row consisting four hours average value.

could you please help me regarding this. this is my table data

this is my result with your query Its not correct right?

Upvotes: 0

Views: 1153

Answers (2)

Andomar
Andomar

Reputation: 238058

select  day
,       (hour - 1) DIV 4  /* or: (h + 3) DIV 4 */
,       avg(value)
from    YourTable
group by
        day
,       (hour - 1) DIV 4

Upvotes: 2

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125204

Exact the same as Andomar's just using the integer division operator:

select  day
,       hour div 4
,       avg(value)
from    YourTable
group by
        day
,       hour div 4

Upvotes: 0

Related Questions