Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

T-SQL to get multiple rows into single row for the same ID group

$
0
0

Hi All,

I have a table with 3 columns and my source data looks like..

ID     Name    Role
1       Sam        TP
1       Tom        TP
1       John        TL
2       Rosy        TP
2       Deb          TL
3       Moorie      TL
3       Jaise         TL
3       Chang       TP

My Output Should look like this..

ID      TP_Name      TL_Name  
1        Sam,Tom          John    
2         Rosy                 Deb
3         Chang           Moorie,Jaise 

Create Statement:

Create Table Sample
(ID Int not Null ,Name Varchar(25) Not Null, Role Varchar(10) Not Null)

Insert Statement:

Insert into Sample (ID,Name,Role) Values (1,'Sam','TP')
Insert into Sample (ID,Name,Role) Values (1,'Tom,'TP')
Insert into Sample (ID,Name,Role) Values (1,'John,'TL')
Insert into Sample (ID,Name,Role) Values (2,'Rosy','TP')
Insert into Sample (ID,Name,Role) Values (2,'Deb','TL')
Insert into Sample (ID,Name,Role) Values (3,'Moorie','TL')
Insert into Sample (ID,Name,Role) Values (3,'Jaise','TL')
Insert into Sample (ID,Name,Role) Values (3,'Chang','TP')

Thanks in Advance,
RH


sql


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>