0

How to find unique values from a column that has multiple values separated by a space.

For example, I have four categories namely A, B, C and D. First two rows have the categories A, B and C and last two rows have categories B, C and D.

Categories

----------

A B C
A B C
B C D
B C D

I want to get the unique values which are A, B, C, D

But if I write the following query it will give me the following result which is not desired:

SELECT DISTINCT categories from table

Categories:

A B C
B C D

Desired result is following:

Categories

-----------------

A
B
C
D
Arslan
  • 51
  • 5
  • 1
    There is no pretty way to do this, but some good options are presented here: [SQL split values to multiple rows](https://stackoverflow.com/questions/17942508/sql-split-values-to-multiple-rows) This functionality is usually called something like "Split string to rows" and each RDBMS has a different way to make this work. Ultimately though, if you find yourself doing this often, it's a very good indicator that your schema is bad and it may be worth the effort to fix your ETL that loads this data instead of writing multiple values into a single column. – JNevill Feb 09 '22 at 14:53

0 Answers0