I would like to get the min, and max, the first and the last record in my database mysql. I'm looking for a high-performance query that takes no more than 15 minutes.
I have tested :
SELECT
MIN(low),
MAX(high),
CONCAT(YEAR(date), "-", WEEK(date)) AS myweek,
(select open from prices m where WEEK(m.date)=WEEK(prices.date) order by date LIMIT 1) as first_open,
(select close from prices m where WEEK(m.date)=WEEK(prices.date) order by date desc LIMIT 1) as last_close
FROM prices
GROUP BY myweek
ORDER BY date;
But i have a error :
Erreur dans la requête (1055): Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'siteinfo.prices.date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
I can not correct this error because I do not have access to conf files and I do not have user superadmin.
I have tested too :
select
DATE_FORMAT(date, "%Y" "%m" "%d") as datetime,
(select open from prices m where m.date like CONCAT(YEAR(datetime), "-[0]", Month(datetime), "-[0]", DAY(datetime), "%") and fk_cryptoid = 'f2a0ba90-93df-11e8-af1b-5968de90d63b' order by date ASC LIMIT 1) as open,
(select close from prices m where m.date like CONCAT(YEAR(datetime), "-[0]", Month(datetime), "-[0]", DAY(datetime), "%") and fk_cryptoid = 'f2a0ba90-93df-11e8-af1b-5968de90d63b' order by date DESC LIMIT 1) as close,
min(low) as low,
max(high) as high
from prices
where fk_cryptoid = 'f2a0ba90-93df-11e8-af1b-5968de90d63b'
GROUP BY datetime;
but CONCAT() does not return the day and the month with a zero in addition: 2018-1-2 and not 2018-01-02. Then this request takes too much time.
Model for table Prices :
id int(11) Incrément automatique
close double NULL
open double NULL
low double NULL
high double NULL
date datetime NULL
createdAt datetime
updatedAt datetime
fk_cryptoid char(36) NULL
If it works for you, then I would suggest the group_concat()
/ substring_index()
trick:
SELECT MIN(low), MAX(high),
CONCAT(YEAR(date), '-', WEEK(date)) AS myweek,
SUBSTRING_INDEX(GROUP_CONCAT(open ORDER BY date), ',', 1) as first_open,
SUBSTRING_INDEX(GROUP_CONCAT(close ORDER BY date DESC), ',', 1) as last_close
FROM prices
GROUP BY myweek
ORDER BY date;
Notes:
first_open
and last_close
columns will be strings. open
and close
do not have commas. If they do, use a different separator. GROUP_CONCAT()
has a default internal limit of about 1,000 bytes. You can adjust that using server parameters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.