The specific update statement depends on the type of column b
, but there are really only 3 different ways this data could be stored, in a delimited string, an text array or a json
The update statement for the comma separated text field would be:
update mytable
set b = array_to_string(array(select distinct unnest(string_to_array(b, ', '))), ', ');
If b
is an text array then:
update mytable
set b = array(select distinct unnest(b));
If b
is a json array then:
update mytable
set b = array_to_json(array(select distinct value from json_array_elements_text(b)));
As you can see, the cleanest statement in is case results from the data being stored as a text array. If you must store an array of values in 1 column, do it using an array type.
However, I would also recommend normalizing your data.
These statements above will update all rows in the table, thus incurring a higher execution cost. I'll illustrate a way to reduce updates using the text array variant (as that requires the shortest sql query):
update mytable
set b = array(select distinct unnest(b))
where array_length(b_array, 1) != (select count(distinct c) from unnest(b) c);