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.

user1829708
  • 73
  • 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,381
  • 1
  • 24
  • 38