Hi I have a table [table1] that contains a list of part numbers in column A, Operation No in column B and the machine we make them on in column C. What I am wanting to do is group on column A and merge the contents of column C with '[ ]' around each one
Hi it is a view I want to create to read only. The table contains thousands of records. I just want the view to group on column A (Part_No), Ignore Column B, and merge column C based on the grouping
select Part_No,
STUFF((SELECT concat('[',b.machine,'] ')
FROM machines b
where a.Part_no = b.Part_no
ORDER BY b.machine
FOR XML PATH('')),1,0,'') AS tag
from machines a
group by Part_No
I would recommend adding TYPE and specifying the data type. For example:
select Part_No,
STUFF((SELECT concat('[',b.machine,'] ')
FROM machines b
where a.Part_no = b.Part_no
ORDER BY b.machine
FOR XML PATH(''), TYPE).value('(./text())[1]', 'varchar(8000)'),1,0,'') AS tag
from machines a
group by Part_No
Including the ./text and the specific data type will help improve performance on larger data sets.