[英]Return the first and last value from one column when value from another column changes
I am trying to write a PostgreSQL query to return the first and last dates corresponding to indices.我正在尝试编写一个 PostgreSQL 查询以返回与索引对应的第一个和最后一个日期。 I have a table:
我有一张桌子:
Datetime![]() |
Index![]() |
---|---|
March 1 2021 ![]() |
0 ![]() |
March 2 2021 ![]() |
0 ![]() |
March 3 2021 ![]() |
0 ![]() |
March 4 2021 ![]() |
1 ![]() |
March 5 2021 ![]() |
1 ![]() |
March 6 2021 ![]() |
2 ![]() |
In this case, I would want to return:在这种情况下,我想返回:
I am wondering how I would write the PostgreSQL query for this.我想知道如何为此编写 PostgreSQL 查询。
I think this can be done with the following:我认为这可以通过以下方式完成:
SELECT MIN("Datetime") AS Start
, MAX("Datetime") AS End
, "Index"
FROM <your_table>
GROUP BY "Index"
ORDER BY "Index"
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.