I have a very simple table called MYTABLE:
TSTAMP VARCHAR(20), VALUE VARCHAR(20)
The timestamp is like yyyy-mm-ddTHH:MM:SS.zzzZ (ex: 2017-04-25T12:23:00.000Z). Value is a float.
I know this table model is just bad but it has been done many years ago by someone else who obviously didn't know what he/she was doing.
I am trying to write an efficient query to get the min(VALUE), the max(VALUE) and their TIMESTAMP over intervals of time (ex: min/max of each minute). I am able to get the min and max value with the following query but I cannot see a way to get their timestamps.
SELECT MIN(tstamp)
, MAX(tstamp)
, MIN(value) minVal
, MAX(value) max
, ABS(TRUNCATE(((UNIX_TIMESTAMP(tstamp)*1000+SUBSTR(tstamp,-4,3)) - 1493115780000)/20000,0)) intervalNumber
, tstamp
FROM mytable
WHERE tstamp BETWEEN '2017-04-25T12:23:00.000Z' AND '2017-04-25T12:24:00.000Z'
AND NOT tstamp = '2017-04-25T12:24:00.000Z'
GROUP
BY intervalNumber;
WHERE 1493115780000 is the result of:
SELECT UNIX_TIMESTAMP('2017-04-25T12:23:00.000Z')*1000+SUBSTR('2017-04-25T12:23:00.000.000Z',-4,3);
*EDIT BECAUSE I WAS NOT CORRECTLY EXPLAINING What i get is for each interval: first timestamp , lasttimestamp ,min value , maxvalue , internval number, first timestamp
What i want is: timestamp of min value, timestamp of max value, min value, max value, interval number
I am using Mysql 5.5.
Any help would be appreciated :) It looks like an university tutorial but this is now too far behind me :(