mysql - Finding changes in value by dates -
i have table this:
datetime | value ----------------------------------- 2015-05-01 12:23:41 | 12 2015-05-01 22:13:11 | 21 2015-05-02 9:13:41 | 23 2015-05-02 17:23:41 | 32 each day has 100 entries @ random time. need calculate following:
- daily increase - difference between first entry of today , first entry of yesterday (for each day)
- daily avg increase - difference between avg value today , avg value yesterday (for each day)
how can write sql query these values?
what first entry of each date group date portion, , minimum datetime this:
select date(datecol), min(datecol) mytable group date(datecol); this see results like:
| 2015-05-01 | 2015-05-01 12:23:41 | once have values, can join 2 tables each date next previous one, this:
select t1.firstdate, t2.seconddate from( select date(datecol) wholedate, min(datecol) firstdate mytable group date(datecol)) t1 join( select date(datecol) wholedate, min(datecol) seconddate mytable group date(datecol)) t2 on t1.wholedate < t2.wholedate , t2.wholedate = (select min(date(datecol)) mytable date(datecol) > date(t1.wholedate)); then, have adjust select statement difference. have join original table values:
select t1.firstdate, t2.seconddate, (s.value - f.value) difference from( select date(datecol) wholedate, min(datecol) firstdate mytable group date(datecol)) t1 join( select date(datecol) wholedate, min(datecol) seconddate mytable group date(datecol)) t2 on t1.wholedate < t2.wholedate , t2.wholedate = (select min(date(datecol)) mytable date(datecol) > date(t1.wholedate)) join mytable f on f.datecol = t1.firstdate join mytable s on s.datecol = t2.seconddate; for second part, you'll need calculate average each date, , join tables in similar way see difference of averages date date.
here sql fiddle example, , here similar question might well.
Comments
Post a Comment