I am trying to write a PostgreSQL query to return the first and last dates corresponding to indices. 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.
CodePudding user response:
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"
;
