How can I group an (unknown) number of rows into a single row where the set columns determine the grouping?
For example, shift
Ref Name Link
==============================
1 John L1
1 John L2
1 John L8
2 Steve L1
2 Steve L234
Into
Ref Name ... ... ...
==========================================
1 John L1 L2 L8
2 Steve L1 L234 NULL
Thanks for any help
If the number of different Links is unkown this needs to be done dynamically. I think this will work as required:
It is a slight varation of the usual PIVOT function because normally
link
would be in the column header. It basically determines the number of columns required (i.e. the maximum distinct values for Link per ref/Name) then Pivots the values into these columns.You might pivot the table using row_number() as a source of column names:
Simply extend the list of numbers if you have more rows.
Live test is @ Sql Fiddle.