微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql PostgreSQL 中的常量插值

如何解决sql PostgreSQL 中的常量插值

我在 Postgresql 中有数据。数据如下:

date,fvalue
18/2/2021 14:05:30.959,46.22347
18/2/2021 14:05:00.939,46.119312
18/2/2021 14:00:00,45.668617
18/2/2021 13:54:01.042,45.997612
18/2/2021 13:53:31.049,45.858727
18/2/2021 13:49:31.019,45.414665
18/2/2021 13:49:01.079,45.518837
18/2/2021 13:43:01.059,45.933113
18/2/2021 13:42:31.039,45.77108
18/2/2021 13:39:01.139,45.3008
18/2/2021 13:38:31.109,45.39339
18/2/2021 13:32:01.139,45.875237
18/2/2021 13:31:31.169,45.701637
18/2/2021 13:27:31.209,45.35322
18/2/2021 13:27:01.169,45.515255
18/2/2021 13:21:31.149,46.00561
18/2/2021 13:21:01.189,45.832012
18/2/2021 13:16:31.179,45.34471
18/2/2021 13:16:01.219,45.506744
18/2/2021 13:15:01.229,45.877106

记录之间的时间间隔不是恒定的。我希望记录之间有 1 秒的时间间隔。如何在带有 SELECT 语句的 Postgresql 查询中做到这一点? 最好的问候,

解决方法

您可以像这样找到给定时间(在本例中为 2021-02-18 13:20:00)的内插值:

WITH prior AS (
   SELECT date,fvalue FROM atable
   WHERE date <= TIMESTAMP '2021-02-18 13:20:00'
   ORDER BY date DESC
   LIMIT 1
),later AS (
   SELECT date,fvalue FROM atable
   WHERE date >= TIMESTAMP '2021-02-18 13:20:00'
   ORDER BY date
   LIMIT 1
)
SELECT prior.fvalue
     + (later.fvalue - prior.fvalue)
       * extract(epoch FROM (TIMESTAMP '2021-02-18 13:20:00') - prior.date)
       / extract(epoch FROM later.date - prior.date)
FROM prior CROSS JOIN later;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。