简体   繁体   English

当另一列的值更改时,返回一列的第一个和最后一个值

[英]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 2021 年 3 月 1 日 0 0
March 2 2021 2021 年 3 月 2 日 0 0
March 3 2021 2021 年 3 月 3 日 0 0
March 4 2021 2021 年 3 月 4 日 1 1个
March 5 2021 2021 年 3 月 5 日 1 1个
March 6 2021 2021 年 3 月 6 日 2 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.

相关问题 需要在一列中查找值,然后从另一列返回值 - Need to find value in one column and then return value from another column 返回依赖于另一列中的值的值 - Return values that relies on a value from another column 根据另一公共列,用另一列中的值更新一列 - Updating one column with the value from another, based on another common column 将值从一列更改为另一列 - Change value from one column into another 通过另一列的键值组合一列中的值 - Combining values from one column by the key value from another column SQL-从一列返回所有唯一的值对,在另一列中返回每个值 - SQL - Return all unique pairs of values from one column, for each value in another column SQL-基于一个表与另一个表的联接(第一个表的行值到第二个表的列值) - SQL - Joining one table with another based on ( Row value from first table to Column value of second table) SQL 将列值作为枚举返回 - 来自另一列的 GetValue() 字段 - SQL Return Column value as Enum - GetValue() field from another Column 如果列值与另一列匹配,则返回最新行 - Return most recent row if column value matches from another column Oracle SQL:至少选择前n行,直到列值与最后一行不同为止 - Oracle SQL: Select at least the first n rows, continue until a column value is different from the last one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM