sql server - How to generate sql output as piped -


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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -