提取与条件PostgreSQL的Postgres上DateTime值有关的最小值和最大值

发布于 2025-02-14 00:20:50 字数 1273 浏览 0 评论 0原文

我正在尝试查询一个表,该表具有随着时间的推移记录的值的集合。我想产生一个结果集,该集合可以抓住第一个日期和最后一个日期,该日期连续> 170。

我认为我需要使用最小功能和最大功能以及一些子来实现这一目标,但是我只是没有得到所需的结果。

有没有有效的方法可以在PostgreSQL上实现下面的结果?

这是我的数据集的示例:

日期
2022-02-07 15:30:30169.6
2022-02-07 15:30:55171
2022-02-07 15:31:10170.9
2022-02-02-07 15: 31:50171.1
2022-02-07 15:32:00172
2022-02-07 15:32:45168
2022-02-07 15:33:20168.7
2022-02-07 15:34:10173.7
2022-02-02-07 15:34:34:55 171.5
2022-02-02-02-07 15:35:20171.7
2022-022-02-02-02-07 15 :36:05163.5

预期结果:

date_startdate_endvalue_max
2022-02-07 15:30:552022-02-07 15:32:00172
2022-02-07 15:34:34:102022-02-07 15:35:35:20 173.7

I am trying to query a table that has a collection of values recorded over time. I would like to produce a result set that grabs the first date and the last date where the value>170 consecutively.

I think I need to use the MIN and MAX functions and some subqueries to achieve this, but I'm just not getting the results I need.

Is there an effective way to achieve the results below on PostgreSQL?

Here's a sample of my data set :

datevalue
2022-02-07 15:30:30169.6
2022-02-07 15:30:55171
2022-02-07 15:31:10170.9
2022-02-07 15:31:50171.1
2022-02-07 15:32:00172
2022-02-07 15:32:45168
2022-02-07 15:33:20168.7
2022-02-07 15:34:10173.7
2022-02-07 15:34:55171.5
2022-02-07 15:35:20171.7
2022-02-07 15:36:05163.5

the expected result:

Date_startDate_endValue_max
2022-02-07 15:30:552022-02-07 15:32:00172
2022-02-07 15:34:102022-02-07 15:35:20173.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静赏你的温柔 2025-02-21 00:20:50

这是一个差距&岛屿问题。您可以使用传统解决方案:使用lag()lead()

例如:

select min(date), max(date), max(value)
from (
  select *, sum(i) over(order by date) as g
  from (
    select *,
      case when (lag(value) over(order by date) > 170) <> (value > 170)
           then 1 else 0
      end as i
    from mytable
  ) x
) y
group by g
having max(value) > 170
order by g

结果:

 min                  max                  max   
 -------------------- -------------------- ----- 
 2022-02-07 15:30:55  2022-02-07 15:32:00  172.0 
 2022-02-07 15:34:10  2022-02-07 15:35:20  173.7 

请参见

This is a Gaps & Islands problem. You can use the traditional solution: using LAG() or LEAD().

For example:

select min(date), max(date), max(value)
from (
  select *, sum(i) over(order by date) as g
  from (
    select *,
      case when (lag(value) over(order by date) > 170) <> (value > 170)
           then 1 else 0
      end as i
    from mytable
  ) x
) y
group by g
having max(value) > 170
order by g

Result:

 min                  max                  max   
 -------------------- -------------------- ----- 
 2022-02-07 15:30:55  2022-02-07 15:32:00  172.0 
 2022-02-07 15:34:10  2022-02-07 15:35:20  173.7 

See running example at db<>fiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文