提取与条件PostgreSQL的Postgres上DateTime值有关的最小值和最大值
我正在尝试查询一个表,该表具有随着时间的推移记录的值的集合。我想产生一个结果集,该集合可以抓住第一个日期和最后一个日期,该日期连续> 170。
我认为我需要使用最小功能和最大功能以及一些子来实现这一目标,但是我只是没有得到所需的结果。
有没有有效的方法可以在PostgreSQL上实现下面的结果?
这是我的数据集的示例:
日期 | 值 |
---|---|
2022-02-07 15:30:30 | 169.6 |
2022-02-07 15:30:55 | 171 |
2022-02-07 15:31:10 | 170.9 |
2022-02-02-07 15: 31:50 | 171.1 |
2022-02-07 15:32:00 | 172 |
2022-02-07 15:32:45 | 168 |
2022-02-07 15:33:20 | 168.7 |
2022-02-07 15:34:10 | 173.7 |
2022-02-02-07 15:34:34: | 55 171.5 |
2022-02-02-02-07 15:35:20 | 171.7 |
2022-022-02-02-02-07 15 :36:05 | 163.5 |
预期结果:
date_start | date_end | value_max |
---|---|---|
2022-02-07 15:30:55 | 2022-02-07 15:32:00 | 172 |
2022-02-07 15:34:34:10 | 2022-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 :
date | value |
---|---|
2022-02-07 15:30:30 | 169.6 |
2022-02-07 15:30:55 | 171 |
2022-02-07 15:31:10 | 170.9 |
2022-02-07 15:31:50 | 171.1 |
2022-02-07 15:32:00 | 172 |
2022-02-07 15:32:45 | 168 |
2022-02-07 15:33:20 | 168.7 |
2022-02-07 15:34:10 | 173.7 |
2022-02-07 15:34:55 | 171.5 |
2022-02-07 15:35:20 | 171.7 |
2022-02-07 15:36:05 | 163.5 |
the expected result:
Date_start | Date_end | Value_max |
---|---|---|
2022-02-07 15:30:55 | 2022-02-07 15:32:00 | 172 |
2022-02-07 15:34:10 | 2022-02-07 15:35:20 | 173.7 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个差距&岛屿问题。您可以使用传统解决方案:使用
lag()
或lead()
。例如:
结果:
请参见
This is a Gaps & Islands problem. You can use the traditional solution: using
LAG()
orLEAD()
.For example:
Result:
See running example at db<>fiddle.