-1

I am trying to create a view that groups by a particular column (let's say BRAND) and all instances of a 2nd column (let's say COLOR) for each BRAND, grouped in a single cell and separated by semicolon. It's hard for me to put it in words but this is what I am looking for:

FROM THIS TABLE

|BRAND  |COLOR  |
|-------|-------|
|TOYOTA |BLUE   |
|NISSAN |BLUE   |
|FORD   |BLUE   |
|NISSAN |RED    |
|NISSAN |GREEN  |
|FORD   |RED    |
-----------------

TO THIS VIEW

|BRAND  |COLOR          |
|-------|---------------|
|TOYOTA |BLUE           |
|NISSAN |BLUE;RED;GREEN |
|FORD   |BLUE;RED       |
|-------|---------------|

I wish I could provide code but I have not come up with any ideas.

Shadow
  • 33,525
  • 10
  • 51
  • 64
alexqr1
  • 13
  • 1
  • 8

1 Answers1

0

You can use group_concat. here is the demo.

select
  brand,
  group_concat(color order by color SEPARATOR ';' ) as color
from myTable
group by
  brand

output:

| brand  | color          |
| ------ | -------------- |
| FORD   | BLUE;RED       |
| NISSAN | BLUE;GREEN;RED |
| TOYOTA | BLUE           |
zealous
  • 7,336
  • 4
  • 16
  • 36