user2693017
user2693017

Reputation: 1870

MySQL get average values per hour from the last 24 hours

I use the following query to get the average values per hour for one day, but I don't get it to result the latest data instead of the oldest:

SELECT id, serverID, AVG(performance) as performance, AVG(online) as online, HOUR(timestamp)
    FROM stats_server
    WHERE serverID= :serverID AND DATE_SUB(`timestamp`,INTERVAL 1 HOUR)
    GROUP BY HOUR(timestamp)
    ORDER BY id ASC

Upvotes: 3

Views: 6088

Answers (1)

R D
R D

Reputation: 1332

You have to mention the last 24 hours data:

SELECT id, serverID, AVG(performance) as performance, AVG(online) as online, 
HOUR(timestamp) FROM stats_server
WHERE serverID= :serverID AND DATE_SUB(`timestamp`,INTERVAL 1 HOUR) And 
timestamp > DATE_SUB(NOW(), INTERVAL 1 DAY)
GROUP BY HOUR(timestamp)
ORDER BY id ASC

Upvotes: 3

Related Questions