stacktiger
stacktiger

Reputation: 21

SQL query to calculate average data

I have a SQLite DB and I want to compress some data from a temperature readings. The readings are recorded every minute. I have no problem to calculate this on a daily base with

"SELECT date(Time_T), AVG(reading) AS reading_avg, MIN(reading) AS reading_min, MAX(reading) AS reading_max FROM readings  WHERE (Zeitpunkt_T > '2016-02-20') GROUP BY (SELECT date(Time_T))"

That works pretty fine and gives me daily averages together with min & max. But this compression is too much. I would like to have values per hour. How can I calculate this ?

Upvotes: 2

Views: 730

Answers (1)

Richard Hamilton
Richard Hamilton

Reputation: 26444

I would like to have values per hour

It looks like you would like to group by the hour.

To get hours in sqlite, use the strftime function and pass in %H as the formatter.

SELECT date(Time_T), AVG(reading) AS reading_avg, MIN(reading) AS reading_min, 
       MAX(reading) AS reading_max 
FROM readings  
WHERE (Zeitpunkt_T > '2016-02-20') 
GROUP BY strftime("%H", date);

https://www.sqlite.org/lang_datefunc.html

Upvotes: 4

Related Questions