简体   繁体   English

sql请求检索每天的最大,最小第一个和最后一个记录

[英]sql request to retrieve each day max, min first and last record

I would like to get the min, and max, the first and the last record in my database mysql. 我想在数据库mysql中获取最小和最大记录。 I'm looking for a high-performance query that takes no more than 15 minutes. 我正在寻找不超过15分钟的高性能查询。

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. 我无法更正此错误,因为我无权访问conf文件并且没有用户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. 但是CONCAT()不会返回日期和月份,且其零为零:2018-1-2,而不是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: 如果它适合您,那么我建议使用group_concat() / substring_index()技巧:

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: 笔记:

  • This first_open and last_close columns will be strings. first_openlast_close列将为字符串。
  • The assumption is that open and close do not have commas. 假设openclose没有逗号。 If they do, use a different separator. 如果是这样,请使用其他分隔符。
  • GROUP_CONCAT() has a default internal limit of about 1,000 bytes. GROUP_CONCAT()的默认内部限制约为1,000个字节。 You can adjust that using server parameters. 您可以使用服务器参数进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM