简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM