| Author |
Topic |
|
prakashdgl
Starting Member
1 Post |
Posted - 2007-09-12 : 03:22:14
|
| Hi, I have a table EmpProject with following: ========================= unitid projectid employees ----------- --------- ----------------------- 1 CCT1 Ravi,Raja,Kanna 2 CCT1 John,Vijay,Nithya 1 CCT2 Senthil,Lee,Suresh 2 CCT2 Ram,Krish,Latha 2 CCT3 Raja,Vijay,Ram 2 CCT4 Sankar TSql to create the above : create table empproject (unitid int, projectid varchar(4), employees varchar(1000)) insert empproject select 1,'CCT1', 'Ravi,Raja,Kanna' insert empproject select 2,'CCT1', 'John,Vijay,Nithya' insert empproject select 1,'CCT2', 'Senthil,Lee,Suresh' insert empproject select 2,'CCT2', 'Ram,Krish,Latha' insert empproject select 2,'CCT3', 'Raja,Vijay,Ram' insert empproject select 2,'CCT4', 'Sankar' I would like to have it in the following format ==================================================== unitid CCT1 CCT2 CCT3 CCT4 --------------------------------------------------------------------------------------------- 1 Ravi,Raja,Kanna Senthil,Lee,Suresh 2 John,Vijay,Nithya Ram,Krish,Latha Raja,Vijay,Ram Sankar Is there any way to query in select statement? Thanks in Advance, Prakash |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2007-09-12 : 08:30:17
|
| Hi,DECLARE @sourceTbl TABLE(ID int,Word varchar(50))insert into @sourceTbl(Id,Word)select 1,'Ch' union allselect 1,'Ranga' union allselect 1,'nath' union allselect 2,'Ch' union allselect 2,'Keerthi' union allselect 2,'sai' union allselect 3,'Ch' union allselect 3,'Anjaneya' union allselect 3,'Kumar'declare @tempWords varchar(200)declare @id int update @sourceTbl set @tempWords = Word = CASE WHEN Id = @id THEN @tempWords + ', ' + Word ELSE Word END , @id = idselect max(Word),id From @sourceTbl group by id |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|