【问题标题】:Find the min value & datetime for each day查找每天的最小值和日期时间
【发布时间】:2021-08-05 20:54:01
【问题描述】:

我有一个 timescaleDB 数据库,其形状如下:

我每 5 分钟有一个温度值。

dt temperature
2019-04-20 20:00:00 13
2019-04-20 20:05:00 12
2019-04-20 20:10:00 12

我想获得每天的最低温度值并显示发生时的确切日期时间。另一个小限制,如果我一天有两个最低温度值,我应该选择一天中较早的那个

答案应该是这样的:

dt temperature
2019-04-20 07:10:00 10
2019-04-21 04:35:00 5
2019-04-22 02:10:00 9

我已经得到了接近这个的东西,唯一缺少的部分是确切的 dt,我只能得到日期。

SELECT DATE(dt), temperature
FROM temperature_DB
GROUP BY CAST(dt as DATE)

提前感谢您的帮助!!

【问题讨论】:

    标签: postgresql datetime max min timescaledb


    【解决方案1】:

    您可以为此使用DISTINCT ON 语法。 要求是日期只有一个值,因此应使用 DISTINCT ON (dt:date)。其他所有内容都在语句的 ORDER BY 部分中进行了描述。

    SELECT 
      DISTINCT ON (dt::date)
      dt,
      temperature
    FROM
      temperature_DB
    ORDER BY
      dt::date,
      temperature ASC, -- (first value will be lowest)
      dt ASC --(put the earliest time of lowest temperature measurement first);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多