0

For example, if I have a data-set as below

ID ITEM
1   A
2   B
3   C
1   B
1   C
2   A

then need a sql query which gives output as below:

ID  ITEM
1    A, B, C
2    B, A
3    C

That is how to group based on ID and display all the values pertaining to another column attributes in SQL.

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
user1829708
  • 63
  • 10

1 Answers1

0

Replace the table names with your table name and use the below query:

Select src.ID,
       src.Items [Items]
    From
        (
            Select distinct T2.ID, 
                (
                    STUFF((Select T1.Item + ',' AS [text()]
                    From [Yourtable-Name] T1
                    Where T1.ID = T2.ID
                    ORDER BY T1.ID
                    For XML PATH ('')),1,1,'')
                ) [Items]
            From [YourTableName] T2
        ) [src]
Jibin Balachandran
  • 3,271
  • 1
  • 21
  • 38