Matt
Matt

Reputation: 15071

Remove duplicate values in the same column in PostgreSQL

I want to remove duplicate values from the same string in a single column.

For examples sake say we have TableA which has a single field FieldA.

Sample Data

FieldA
red blue pink purple green green green yellow pink purple blue blue blue

Expected Output

FieldA
red blue pink purple green yellow

Upvotes: 0

Views: 80

Answers (1)

Frank Heikens
Frank Heikens

Reputation: 127106

First of all you should imho fix your schema, your current schema will cause many more issues, not just this one.

But to answer the question, you could use some array functions:

SELECT  array_to_string(array_agg( distinct content), ' ')
FROM    unnest(string_to_array('red blue pink purple green green green yellow pink purple blue blue blue',' ')) u(content);

Upvotes: 1

Related Questions