MySQL Forums
Forum List  »  Newbie

Re: Average on different intervals
Posted by: Peter Brawley
Date: May 03, 2015 12:10PM

SQL doesn't handle time values at all well, so this isn't a beginner's problem.

Your averaging period extends across two calendar days, and that seriously complicates the query unless the data table has a column denoting the calculation reference day for the value, eg ...

+------+---------------------+------------+-------+
| id   | ts                  | calcday    | value |
+------+---------------------+------------+-------+
|    1 | 2015-05-01 10:00:00 | 2015-05-01 |  3.00 |
|    2 | 2015-05-01 13:59:59 | 2015-05-01 |  3.50 |
|    3 | 2015-05-01 06:00:00 | 2015-05-01 |  4.00 | -- time < 06:59:59, so calcday is previous day
|    4 | 2015-05-02 10:00:00 | 2015-02-01 |  5.00 |
+------+---------------------+------------+-------+

Then this fetches your 9am-7pm averages ...

select calcday, avg(value)
from t
where ( calcday=date(ts) and time(ts)>='09:00:00' ) or ( calcday=date(ts)-interval 1 day and time(ts)<'07:00:00' )
group by calcday;

... and this fetches the 12-2pm averages ...

select calcday, avg(value)
from t
where time(ts) between '12:00:00' and '13:59:59'
group by calcday;

Options: ReplyQuote


Subject
Written By
Posted
Re: Average on different intervals
May 03, 2015 12:10PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.