Created
October 12, 2017 19:06
-
-
Save purpleP/078be048a63b8a5311d58ea95bc4ab08 to your computer and use it in GitHub Desktop.
Minumum, maximum and first and last from group without `first` and `last` in mysql (it doesn't have them)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table `values` ( | |
id integer unsigned primary key auto_increment, | |
value double, | |
`time` time | |
); | |
insert into `values` (time, value) values | |
('00:00:01', 3), | |
('00:00:02', 2), | |
('00:00:03', 1), | |
('00:01:01', 4), | |
('00:01:02', 5), | |
('00:01:03', 6); | |
SELECT | |
vs1.value as `open`, | |
vs2.value as `close`, | |
te.minv as `min`, | |
te.maxv as `max` | |
FROM | |
( | |
SELECT | |
MIN(time) as mint, | |
MAX(time) as maxt, | |
MIN(value) as minv, | |
MAX(value) as maxv | |
FROM `values` | |
GROUP BY hour(time), minute(time) | |
) as te | |
JOIN `values` vs1 ON vs1.time = te.mint | |
JOIN `values` vs2 ON vs2.time = te.maxt | |
ORDER BY vs1.time; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment