0

我有这个查询。返回每日最低和最高温度:

select  year(DateTimeS),month(DateTimeS),day(DateTimeS),min(Low),max(High) from temperature
where year(DateTimeS) = 2018
group by year(DateTimeS),month(DateTimeS),day(DateTimeS)

我在这个查询中缺少两个字段,LowTime 和 MaxTime。我不知道如何获得最小(低)和最大(高)发生的时间。(DateTimeS 是一个 DateTime 字段,其余为十进制)该表有分分钟的温度数据,如下所示:

+-----------------------+--------+-------+
|      "DateTimeS"      | "High" | "Low" |
+-----------------------+--------+-------+
| "2018-09-07 23:58:00" | "89"   | "87"  |
| "2018-09-07 23:57:00" | "88"   | "85"  |
| "2018-09-07 23:56:00" | "86"   | "82"  |
|        .              |        |       |
|        etc...         |        |       |
+-----------------------+--------+-------+

有任何想法吗?

4

1 回答 1

1

在 MariaDB 10.3 中,您应该能够使用窗口函数。所以:

select year(DateTimeS), month(DateTimeS), day(DateTimeS),
       min(Low), max(High),
       max(case when seqnum_l = 1 then DateTimeS end) as dateTimeS_at_low,
       max(case when seqnum_h = 1 then DateTimeS end) as dateTimeS_at_high
from (select t.*,
             row_number() over (partition by date(DateTimeS) order by low) as seqnum_l,
             row_number() over (partition by date(DateTimeS) order by high desc) as seqnum_h
      from temperature t
     ) t
where year(DateTimeS) = 2018
group by year(DateTimeS), month(DateTimeS), day(DateTimeS);
于 2018-09-27T00:21:32.083 回答