简体   繁体   中英

Aggregating/Grouping a set of rows/records in MySQL

I have a table say "sample" which saves a new record each five minutes. Users might ask for data collected for a specific sampling interval of say 10 min or 30 min or an hour.

Since I have a record every five minutes, when a user asks for data with a hour sample interval, I will have to club/group every 12 (60/5) records in to one record (already sorted based on the time-stamp), and the criteria could be either min/max/avg/last value.

I was trying to do this in Java once I fetch all the records, and am seeing pretty bad performance as I have to iterate through the collection multiple times, I have read of other alternatives like jAgg and lambdaj, but wanted to check if that's possible in SQL (MySQL) itself.

The sampling interval is dynamic and the aggregation function (min/max/avg/last) too is user provided.

Any pointers ?

You can do this in SQL, but you have to carefully construct the statement. Here is an example by hour for all four aggregations:

select min(datetime) as datetime,
       min(val) as minval, max(val) as maxval, avg(val) as avgval,
       substring_index(group_concat(val order by datetime desc), ',', 1) as lastval
from table t
group by floor(to_seconds(datetime) / (60*60));

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