stUrb
stUrb

Reputation: 6822

Calculating the tendency of a value over time

I've got a table with (amongst other values) the temperature for the last 4 hours. When making a graph of it I can see the 'tendency' of the graph on an eye blink:

enter image description here

The thick red line obviously has a negative direction. While the green line has a positive direction.

How can I calculate this 'direction' value of the last 3 hours worth of data. This data can be retreived from the database with the following sql-statement:

SELECT temp FROM weather WHERE time_utc => NOW() - INTERVAL 3 HOUR

Is there a function like AVG() or something to calculate this or am I overthinking this?

Upvotes: 1

Views: 192

Answers (1)

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48177

What about this :

  SELECT HOUR(time_utc) as  hour_group, AVG(temp) 
  FROM weather 
  WHERE time_utc => NOW() - INTERVAL 3 HOUR
  GROUP BY hour_group

This way you divide your measures in hour block and can compare the first with the last?

Upvotes: 1

Related Questions