My tables:
hourly_weather electrical_readings
---------------- -----------------------
meter | time_read | temp meter | time | kwh
---------------- -----------------------
1 1316044800 55 1 1316136250 19.24
1 1316138400 56 1 1316044320 18.29
(...) (...)
I want to retrieve two important values from this data:
1) I want the total KW for a given day
2) And I want the max temperature for that day
The query I'm using takes WAYYYY too long to run but I can't think of another way to do it. Like, several hours for 100,000 rows of data in both tables.
SELECT * FROM (
SELECT * , SUM(kwh) AS sumkwh,
DATE( FROM_UNIXTIME( r.time_read ) ) AS datex,
UNIX_TIMESTAMP( DATE( FROM_UNIXTIME( r.time_read ) ) ) AS datey,
(
SELECT MAX( temp )
FROM hourly_weather hw
WHERE hw.meter = 1
AND time_read >= datey
AND time_read < datey + 86400
) AS temp
FROM electrical_readings r
WHERE id = 1
GROUP BY datex
) as t1
WHERE t1.temp != '';