it works over your small data set. not sure if you can get rid of the distinct with so many columns you are grouping by.
here is the solution:
WITH RN AS (SELECT ROW_NUMBER () OVER (ORDER BY pms_id) RN1,
ROW_NUMBER() OVER (PARTITION BY eq_id,bl_id,fl_id,rm_id ORDER BY pms_id) RN2,
*
FROM #mytable)
SELECT DISTINCT RN1 - RN2 OrderColumn, eq_id,bl_id,fl_id,rm_id,
REPLACE(STUFF((SELECT ', ' + instructions
FROM RN i
WHERE i.RN1 - RN2 = o.RN1 - o.RN2
FOR XML PATH('')),1,2,''),',',' &')
FROM RN o
ORDER BY RN1 - RN2
if you do not care about the order you can omit the OrderColumn from the result set and remove the order by clause
EDIT: Thanks for the DDL and Sample data on your first post.
EDIT2: Explaining the code:
the first CTE gives us 2 row numbers we can subtract and the result of the math stays the same over the different groups.
the select statement selects our grouping id and then using FOR XML PATH concantenates the instructions together along with a ,. i use a , because it will stay the same in the conversion to XML and then finally the replace changes the , to the requested &
For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]
Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
Jeff Moden's Cross tab and Pivots Part 1[/url]
Jeff Moden's Cross tab and Pivots Part 2[/url]