0

How do I SELECT only the last key from an array column?

SELECT * FROM table;

+----+---------------+
| id | col_array     |
+----+---------------+
| 1  | {"a"}         |
+----+---------------+
| 2  | {"a","b","c"} |
+----+---------------+
| 3  | {"b","d","f"} |
+----+---------------+

The desired result for col_array would be: a, c, f.

1 Answer 1

1

Using array_upper

select col_array [array_upper(col_array, 1)] from my_table
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I should have realized arrays had their own functions in PostgreSQL, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.