I have a join query that returns data like this :
Column A | Column B | Column C | Column D | Column E | Column F | Column G | CONCAT_WS(',',D,E,F,G) |
---|---|---|---|---|---|---|---|
id1 | id2 | id3 | 14 | 15 | 14 | 16 | 14,15,14,16 |
-------- | -------- | -------- | -------- | -------- | -------- | -------- | ---------------------- |
id5 | id6 | id7 | 14 | 15 | 17 | 16 | 14,15,17,16 |
I want the last column to only return unique values found in each record. For example, for the first record I want the last column to have values: (14,15,16)
instead of 14,15,14,16
.
sql is row based rather than column based so there is no function which can operate on columns in the way you want.
If you have a unique row identifier you could UNPIVOT using UNION which comes with an implicit distinct , group_concat and join for example
if you don't have a unique row identifier you might get away with assigning one but there's no guarantee the rows output will be in the same order as the rows input.
Both solution look poor performers but that's what happens with poorly built data.