Skip to content

Instantly share code, notes, and snippets.

@purpleP
Created October 12, 2017 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purpleP/078be048a63b8a5311d58ea95bc4ab08 to your computer and use it in GitHub Desktop.
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)
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