Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从表中选择特定日期的最高温度_Mysql_Sql_Greatest N Per Group - Fatal编程技术网

Mysql 从表中选择特定日期的最高温度

Mysql 从表中选择特定日期的最高温度,mysql,sql,greatest-n-per-group,Mysql,Sql,Greatest N Per Group,我有一个包含以下数据的表格 Temperature DateTimeValue WarnCrit 29.1 2020-06-22 10:08:30 0 29.2 2020-06-22 09:38:28 0 29.2 2020-06-22 09:08:26 0 28.9 2020-06-22 08:38:26 0 28.7 2020-06-22

我有一个包含以下数据的表格

Temperature    DateTimeValue         WarnCrit
29.1        2020-06-22 10:08:30         0
29.2        2020-06-22 09:38:28         0
29.2        2020-06-22 09:08:26         0
28.9        2020-06-22 08:38:26         0
28.7        2020-06-22 08:08:24         0
28.7        2020-06-22 07:38:22         0
29.2        2020-06-22 07:08:21         0
29.8        2020-06-22 06:38:20         0
29.9        2020-06-22 06:08:18         0
我喜欢进行选择,以找到特定日期的最高、最低、平均温度,因此我使用以下方法:

SELECT max(Temperature) as maxtemp
     , min(Temperature) as mintemp
     , avg(Temperature) as avtemp 
  FROM TempHistory 
 WHERE date(DateTimeValue)='2020-06-22'
这项工作是正确的,但我也想有这个温度发生的具体时间。所以我把它改成:

SELECT * 
  from TempHistory 
 where DateTimeValue = '2020-06-22' 
   and Temperature = (select max(Temperature) from TempHistory)

如果没有领带或者你不在乎它们,你可以这样写:

select t.* 
from TempHistory t
where t.DateTimeValue = (
    select t1.DateTimeValue
    from TempHistory t1
    where t1.DateTimeValue >= '2020-06-22' and t1.DateTimeValue < '2020-06-23'
    order by Temperature desc
    limit 1
)
理由:

你的日期有时间段,所以你需要一个不平等过滤器

使用返回最高温度的日期的子查询比返回温度本身更简单,因此,不需要在外部查询中筛选日期


如果您想要当天温度最低的行,只需从order by子句中删除DESC即可。

您可以使用窗口函数,尤其是first_值:

不幸的是,MySQL和SQL通常没有第一个或最后一个聚合函数。然而,这是非常相似的


还要注意WHERE的更改。这允许查询使用DateTimeValue上的索引(如果有可用的话)。

子查询也需要日期。我尝试了一下,但仍然没有返回任何内容,谢谢您的格式化!我尝试了这个,我得到了这个错误MySQL说:文档1051-未知表'database.t'我也想问,为什么不在where上使用=呢?其中datet1.DateTimeValue='2020-06-21'?@GKal:yes datet1.DateTimeValue='2020-06-21'是可能的,但使用半开放间隔进行筛选比使用日期函数更有效。@GKal因为函数不能使用indexes@GMB请问你的答案和下面的有什么不同?从TempHistory t1中选择*,其中t1.DateTimeValue>='2020-06-22'和t1.DateTimeValue<'2020-06-23'按温度说明限制1woow排序!!!我没想到会发生这样的事!非常感谢,我会立即尝试,我会一遍又一遍地阅读有关distinct的文章,但有可能尝试和解释一下吗?再次感谢你!!此外,我是否可以使用以下公式获取月份和年份的值?其中DateTimeValue>='2020-06'和DateTimeValue<'2020-07';我建议使用完整日期:“2020-06-01”和“202-07-01”作为常数。我认为MySQL支持部分日期,但不需要。我使用您的解决方案,但我会出现以下错误。错误静态分析:分析期间发现42个错误。先前已找到别名。在位置44的maxtemp附近,预期会出现别名。在43号位置附近。在位置44的maxtemp附近出现意外标记。靠近,在位置51处未识别的关键字。在位置61处接近最小值。位置64附近有意外标记。位置接近温度65@GKal . . . 我不知道你在使用什么工具,但是代码中绝对没有42个错误。我可能猜该工具无法识别窗口函数。
SELECT DISTINCT max(Temperature) OVER () as maxtemp,
       min(Temperature) OVER () as mintemp,
       avg(Temperature) OVER () as avtemp,
       FIRST_VALUE(DateTimeValue) OVER (ORDER BY Temperature ASC) as dt_at_min,
       FIRST_VALUE(DateTimeValue) OVER (ORDER BY Temperature DESC) as dt_at_max
FROM TempHistory 
WHERE DateTimeValue >= '2020-06-22' AND
      DateTimeValue < '2020-06-23';