sql server - How to generate sql output as piped -
this question has answer here:
- how use group concatenate strings in sql server? 16 answers
- concat groups in sql server [duplicate] 5 answers
i using ms sql server.
i have table of students this:
studentid,teacher 123,adams 124,adams 125,johnson 126,johnson 127,adams 128,marks
i need have output looks this:
teacher,students adams,123|124|127 johnson,125|126 marks,128
i appreciate help. thank you.
declare @table_var table( student_id int, teacher varchar(50) ) insert @table_var select 123, 'adams' union select 124, 'adams' union select 125, 'johnson' union select 126, 'johnson' union select 127, 'adams' union select 128, 'marks' select * @table_var select teacher, students = stuff((select '|' + convert(varchar, t2.student_id) @table_var t2 t1.teacher = t2.teacher xml path('')) ,1,1,'') @table_var t1 group t1.teacher
Comments
Post a Comment