I have an array in Postgres that contains timestamps. I’d like to count the number of entries in the array newer than a timestamp specified in a query. As a bonus, I’d like to delete entries in the array older than another specified timestamp. Can this be done just using SQL in PostgreSQL?
Question posted in PostgreSQL
The official documentation can be found here.
The official documentation can be found here.
2
Answers
To answer your first question,
You can use
UNNEST
function to expand an array into rows and then applyWHERE
clause to filter the array elements.Replace
id
column with any unique column in your table.For the bonus question, the below query will update the array column removing timestamps older than ‘1900-01-01’.
I would use a scalar sub-query: