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)
 Joining rows into a single field

Author  Topic 

cwhite85
Starting Member

6 Posts

Posted - 2003-10-06 : 19:32:02
I have an education database with the following tables:

Class
-------
Class_ID
Class_Name


Class_Instructor
----------------
Class_Instructor_ID
Class_ID
Instructor_Name


If I have two instructors for a class how can I create just one row with the class name and instructor (the instructor field would have both the instructors for that class seperated by a comma).

For example:

Class_ID Class_Name
-------- ----------
1 English

Class_Instructor_ID ClassID Instructor_Name
------------------- ------- ---------------
1 1 Tom Jones
2 1 Pete Moss

would return:

Class_ID Class_Name Instructor
-------- ---------- --------------------
1 English Tom Jones, Pete Moss


Sorry if this is too easy to ask, but I just cant figure it out.

Thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-10-06 : 19:42:52
Here ya go:


create table #workingtable (
i int not null,
vc varchar(50) not null,
list varchar(8000),
constraint pk_wt primary key (i,vc) )

insert into #workingtable (i,vc)
SELECT c.Class_ID AS Class_ID, ci.Instructor_Name AS Instructor_Name
FROM CLASS c
INNER JOIN Class_Instructor ci ON c.Class_ID = ci.Class_ID
ORDER BY c.Class_ID

declare
@list varchar(8000),
@lasti int

select
@list = '',
@lasti = -1

--here is the meat of the work
update
#workingtable
set
@list = list = case
when @lasti <> i then vc
else @list + ', ' + vc
end,
@lasti = i

--return a sample from the final rowset
select top 10
i,
case
when len(max(list)) > 50 then convert(varchar(50), left(max(list),47) + '...')
else convert(varchar(50),max(list))
end as Instructor_Name
from
#workingtable
group by
i
order by
newid()
go

DROP TABLE #workingtable



Here is the link to the article that this solution is based on:

[url]http://www.sqlteam.com/item.asp?ItemID=11021[/url]

Tara
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-10-06 : 19:44:31
BTW, next time please post your tables and data like this:


SET NOCOUNT ON

CREATE TABLE CLASS
(
Class_ID INT NOT NULL,
Class_Name VARCHAR(50) NOT NULL
)

CREATE TABLE Class_Instructor
(
Class_Instructor_ID INT NOT NULL,
Class_ID INT NOT NULL,
Instructor_Name VARCHAR(50) NOT NULL
)

INSERT INTO CLASS VALUES(1, 'English')
INSERT INTO CLASS VALUES(2, 'Math')
INSERT INTO CLASS VALUES(3, 'Science')

INSERT INTO Class_Instructor VALUES(1, 1, 'Tom Jones')
INSERT INTO Class_Instructor VALUES(2, 1, 'Pete Moss')

SELECT c.Class_ID, ci.Instructor_Name
FROM CLASS c
INNER JOIN Class_Instructor ci ON c.Class_ID = ci.Class_ID

DROP TABLE Class
DROP TABLE Class_Instructor



Tara
Go to Top of Page

cwhite85
Starting Member

6 Posts

Posted - 2003-10-07 : 12:49:29
Thanks Tara,

Sorry I asked a question that had already been answered.

And next time I will display the table info correctly.
Go to Top of Page
   

- Advertisement -