-1

Thanks in advance.

I have a table with values like this

class_id Instructor_Name

———————————

1 Joe 

2 Joe 

3 Joe 

1 Judy 

2 Judy 

2 Kevin 

3 Kevin 

and I want the Result Set like this using SQL statement:

id name services (I want)

———————————–

1 Joe, Judy

2 Joe, Judy, KevinC

3 Joe, Kevin

How can I get this

Thanks, Brijesh

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
Brijesh
  • 109
  • 1
  • 2
  • 10

1 Answers1

6

Use STUFF and FOR XML PATH with correlated subquery:

select 
    class_id,
    stuff(
        (select ', ' + Instructor_Name from your_table b 
         where a.class_id = b.class_id for xml path('')),
        1, 2, ''
    ) names
from your_table a
group by
    class_id;
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
  • Thanks a lot, GurV, worked like a charm. I was trying to find the previously posted question but was not able to find the right keyword for this. Thanks Again. – Brijesh Feb 08 '17 at 19:26