Reputation: 29
I have this table:
| arrs |
|:----|
| {10000,10000,10000,10000} |
| {100,200,300} |
| {400,500,600} |
| {100,200,600} |
How get this result?
| uniques |
|:-------|
| 10000 |
| 100 |
| 200 |
| 300 |
| 400 |
| 500 |
| 600 |
Or this result?
| arrUniques |
|:----|
| {10000,100,200,300,400,500,600} |
See -> fiddle
Upvotes: 0
Views: 63
Reputation: 72194
Strictly speaking it's better to place the unnest
into the FROM
part, as putting it into the SELECT
has some weird effects in some cases.
SELECT DISTINCT
a.value AS uniques
FROM yourTable t
CROSS JOIN LATERAL unnest(t.arrs) AS a(value);
Upvotes: 0
Reputation: 543
You can use Distinct unnest
to display unique results as per your first scenario :
SELECT DISTINCT unnest(arr) AS uniques
FROM "test";
For your second scenario displaying result in single array you can add array_agg
:
SELECT array_agg(DISTINCT unnest(arr)) AS arrUniques
FROM "test";
Upvotes: 1
Reputation: 522506
You should be able to use UNNEST()
along with DISTINCT
here:
SELECT DISTINCT UNNEST(arrs) AS uniques
FROM yourTable;
Upvotes: 2