php - Get average of values for a specific time frame and specific entry types -
i have table this:
id | type | value | timestamp -------------------------------- 1 | aaa | 0.5 | day 1 hour 1 2 | bbb | 1.5 | day 1 hour 1 3 | ccc | 1.8 | day 1 hour 1 ..... 11 | aaa | 0.6 | day 1 hour 2 12 | bbb | 1.4 | day 1 hour 2 13 | ccc | 1.5 | day 1 hour 2 ..... id := int, pk, ai, , on type := string/varchar value := double timestamp := unix timestamp (int), changed date type
what want average specific timeframes. example:
i want aaa , bbb average of "value" day 1 till day 3. every day has 24 entries per type on several days.
expected result be
type | average | timestamp/date ------------------------------- aaa | 0.5242 | day 1 aaa | 0.5442 | day 2 aaa | 0.5913 | day 3 bbb | 1.4228 | day 1 bbb | 1.6924 | day 2 bbb | 1.3018 | day 3
i'm not sure if possible mysql. maybe it's more efficient php?
does produce result require?
# drop table if exists likethis; create table likethis (id int unsigned, `type` char(255), `value` decimal(2,1), `timestamp` char(255)); insert likethis values (1, 'aaa', 0.5, 'day 1 hour 1'), (2, 'bbb', 1.5, 'day 1 hour 1'), (3, 'ccc', 1.8, 'day 1 hour 1'), (11, 'aaa', 0.6, 'day 1 hour 2'), (12, 'bbb', 1.4, 'day 1 hour 2'), (13, 'ccc', 1.5, 'day 1 hour 2'); select * likethis; select type, avg(`value`) average, left(`timestamp`, instr(`timestamp`, 'hour') - 2) timestampdate likethis group `type`, left(`timestamp`, instr(`timestamp`, 'hour') - 2);
let me know if not,
regarding how adapt query if date stored in differing field types, here example queries:
# how aggregate day on datetime field type timestamp select type, avg(`value`) average, date_format(`timestamp`, '%y%m%d') timestampdate likethis group `type`, date_format(`timestamp`, '%y%m%d'); # how aggregate day on unix timestamp (stored int) field type timestamp select type, avg(`value`) average, `timestamp` - `timestamp` % 86400 timestampdate likethis group `type`, `timestamp` - `timestamp` % 86400;
thanks,
james
Comments
Post a Comment