I have a table of the following format:
| Ref | OtherRef |
|---|---|
| Fruit | Apple |
| Fruit | Banana |
| Fruit | Lemon |
| Vegetable | Carrot |
| Vegetable | Potato |
What I'm trying to do is flatten this out so it looks like the following:
| Ref | OtherRefA | OtherRefB | OtherRefC |
|---|---|---|---|
| Fruit | Apple | Banana | Lemon |
| Vegetable | Carrot | Potato |
I'm having trouble due to the dynamic nature of the additional columns as the amount of OtherRef that relate to Ref varies.
Anyone able to advise how I am able to achieve this? I'm using SQL Server 2014.
A possible solution is a dynamic statement (... with slightly different column names):
Result: