SQL Server Pivot to unknown number of columns

Viewed 1237

I have data that looks like this:

   id    | serialNo
 0245DS6 | 05813542
 0245DS6 | 05813543
 0245DS6 | 05813544
 2231VC7 | 06885213
 5432PS1 | 01325131
 5432PS1 | 01325132

And I need to output it like this:

    id    | serial_1 | serial_2 | serial_3 | ...
  0245DS6 | 05813542 | 05813543 | 05813544 | ...
  2231VC7 | 06885213 |          |          | 
  5432PS1 | 01325131 | 01325132 |          | 

I don't know how many serial numbers there are per id (will most likely not be greater than 10), and the number varies for each id. I think pivot is what I need to use, but I don't know enough about SQL to know what answers to other questions are useful for me, or if this is even possible.

1 Answers
Related