Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 common column values in a row

Author  Topic 

varmamkm
Starting Member

8 Posts

Posted - 2008-01-18 : 06:03:16
I have a table with the following information
CREATE TABLE TEST2 (ID1 INT, NAM1 VARCHAR (20), PLANT VARCHAR (20))
INSERT INTO TEST2 SELECT 1,'AA1','P1'
INSERT INTO TEST2 SELECT 1,'AA1','P2'
INSERT INTO TEST2 SELECT 1,'AA1','P3'
INSERT INTO TEST2 SELECT 1,'AA1','P4'
INSERT INTO TEST2 SELECT 1,'AA1','P5'
INSERT INTO TEST2 SELECT 2,'AA2','P1'
INSERT INTO TEST2 SELECT 2,'AA2','P2'

can anybody help me out to get the result set like this:

ID1 NAME1 PLANTS
--- ------ ----------------
1 AA1 P1, P2, P3, P4, P5
2 AA2 P1, P2

thanks in advance

kiruthika
Yak Posting Veteran

67 Posts

Posted - 2008-01-18 : 07:22:01
quote:
Originally posted by varmamkm

I have a table with the following information
CREATE TABLE TEST2 (ID1 INT, NAM1 VARCHAR (20), PLANT VARCHAR (20))
INSERT INTO TEST2 SELECT 1,'AA1','P1'
INSERT INTO TEST2 SELECT 1,'AA1','P2'
INSERT INTO TEST2 SELECT 1,'AA1','P3'
INSERT INTO TEST2 SELECT 1,'AA1','P4'
INSERT INTO TEST2 SELECT 1,'AA1','P5'
INSERT INTO TEST2 SELECT 2,'AA2','P1'
INSERT INTO TEST2 SELECT 2,'AA2','P2'

can anybody help me out to get the result set like this:

ID1 NAME1 PLANTS
--- ------ ----------------
1 AA1 P1, P2, P3, P4, P5
2 AA2 P1, P2

thanks in advance


Hi!
Try this

create function myfun (@id int)
returnS varchar(1000)
as
begin
declare @plant varchar(1000)
select @plant=coalesce(@plant+',','')+plant from test2 where id1=@id
return @plant
end

select distinct id1,nam1,dbo.myfun(id1) as plants from test2

kiruthika!
http://www.ictned.eu
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-18 : 07:43:10
For more info refer http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/rowset-string-concatenation-which-method-is-best.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -