0

I have following table in PostgreSQL 11.0

forms
ampoules, capsules, coated tablets, infusion ampoules, liquids, powders, tablets
capsules, coated tablets, powders, tablets, unspecified
coated tablets

I would like to replace certain strings in this comma separated values for each row and get distinct values in the end. For eg. coated tablets by tablets, capsules by tablets, infusion ampoules by ampoules.

The desired output is:

forms_normalized
ampoules, liquids, powders, tablets
tablets, powders, unspecified
tablets

I have no starting point to resolve this issue. Any help is highly appreciated.

Thanks

1 Answer 1

1

One way to do it, is to expand the comma separated list to a "table", then aggregate the distinct values back and replace the ones you want.

select d.id, 
       (select string_agg(distinct case t.form
                             when 'coated tablets' then 'tablets'
                             when 'infusion ampoules' then 'ampoules'
                             else t.form
                           end, ',')
        from regexp_split_to_table(d.forms, '\s*,\s*') as t(form))
from data d
Sign up to request clarification or add additional context in comments.

2 Comments

It worked perfectly fine. Thanks !! How can I use multiple strings to be replaced? For eg. coated tablets by tablets, capsules by tablets. I modified the input for clarity.
Just add to the when conditions. ".., when 'coated tablets' then 'tablets' when 'capsules' then 'tablets' when ... . The problem arises not that I have multiple items which are different things, identified the same, and I do not know which is which. If the combination is the goal, then fine, but be careful when combine different things to the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.