ruby on rails - Grouping by 4 Hours Intervals -
i'm trying average of data have in 4 hours intervals, app in ruby on rails, don't know if should write postgresql query or in model of application, nor don't know how in either. simple version of model this:
id heart_rate timestamp
i need 1 hour, , 1 day interval know how that, on sql side @ least:
select avg(heart_rate), to_char(timestamp, 'yyyy-mm-dd') ts vital_signs group ts order ts asc;
24 hrs / 4 = 6 segments.
convert date hours, integer division (div). give 0-5 numbers. each remainder representing segment.
this example mysql (and not rails), replace div (integer division function of mysql) postgress equivalent.
select avg(heart_rate), hour(timestamp) div 4, count(*) vital_signs group date(timestamp), hour(timestamp) div 4;
you can group on date seperately, per day getting avg. results.
you can execute sql directly within rails via:
activerecord::base.connection.execute "<sql>"
Comments
Post a Comment