Skip to content
Advertisement

Grouping together results of multiple GROUP_CONCAT() with distinct values only

second attempt at this question with more detail. I am attempting to group the distinct values of multiple columns together for objects of the same name. I can use GROUP_CONCAT on each of the ‘Type’ columns, but I can’t merge them together to get a distinct set of values for each name

Here is a sample of my data:

And here is the basic query I have to come up with that does NOT work as desired:

The above query returns:

What I need it to return is a grouping of the groups with distinct values only:

Is this possible?

Advertisement

Answer

One option is to unpivot the columns to rows before grouping. In MySQL, you can do this with union all:

Demo on DB Fiddle:

company  | res  
:------- | :----
Generic  | 1,2,3
Generic2 | 1,2  
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement