Reputation: 8054
I have this query:
SELECT DISTINCT
ces.CourseEventKey,
up.Firstname + ' ' + up.Lastname
FROM InstructorCourseEventSchedule ices
INNER JOIN CourseEventSchedule ces ON ces.CourseEventScheduleKey = ices.MemberKey
INNER JOIN UserProfile up ON up.UserKey = ices.UserKey
WHERE ces.CourseEventKey IN
(
SELECT CourseEventKey
FROM @CourseEvents
)
ORDER BY CourseEventKey
It produces this result set:
CourseEventKey Name
-------------- --------------------
30 JACK K. BACKER
30 JEFFREY C PHILIPPEIT
30 ROBERT B. WHITE
33 JEFFREY C PHILIPPEIT
33 KENNETH J. SIMCICH
35 JACK K. BACKER
35 KENNETH J. SIMCICH
76 KENNETH J. SIMCICH
90 BARRY CRANFILL
90 KENNETH J. SIMCICH
The data is accurate, but I need the result set to look like this:
CourseEventKey Name
-------------- --------------------
30 JACK K. BACKER; JEFFREY C PHILIPPEIT; ROBERT B. WHITE
33 JEFFREY C PHILIPPEIT; KENNETH J. SIMCICH
35 JACK K. BACKER; KENNETH J. SIMCICH
76 KENNETH J. SIMCICH
90 BARRY CRANFILL; KENNETH J. SIMCICH
I've seen questions like mine with working solutions, but I cannot for the life of me adapt those solutions to work with my data.
How can I change my query to produce the 2nd result set using some form of concatenation?
Thanks in advance.
Upvotes: 2
Views: 8712
Reputation: 8865
select distinct ces.CourseEventKey,STUFF((SELECT ', ' +up.Firstname + ' ' + up.Lastname) AS Name
FROM UserProfile up
where UP.id = UserKey = ices.UserKey
FOR XML PATH (''))
, 1, 1, '') AS Name) FROM InstructorCourseEventSchedule ices
INNER JOIN CourseEventSchedule ces ON ces.CourseEventScheduleKey = ices.MemberKey
WHERE ces.CourseEventKey IN
(
SELECT CourseEventKey
FROM @CourseEvents
)
Upvotes: 0
Reputation: 10013
You make a function that takes as a parameter of CourseEventScheduleKey and returns a concatenated string of the users. Then you can use it like:
select CourseEventScheduleKey,
dbo.getUsersForCourse(CourseEventScheduleKey) as Users
from CourseEventSchedule
order by CourseEventScheduleKey
This should return what you wants. The function looks like:
create function getUsersForCourse(@CourseEventScheduleKey int)
returns varchar(max)
as
begin
declare @ret varchar(max)
set @ret = ''
select @ret = @ret + up.Firstname + ' ' + up.Lastname + '; '
from CourseEventSchedule ces
inner join InstructorCourseEventSchedule ices
on ces.CourseEventScheduleKey = ices.MemberKey
inner join UserProfile up
on up.UserKey = ices.UserKey
where ces.CourseEventScheduleKey = @@CourseEventScheduleKey
order by up.Lastname, up.Firstname
if(@ret = '')
return @ret
-- trim off the last semi colon and space
return substring(@ret, 1, len(@ret) - 2)
end
Upvotes: 0
Reputation: 43023
You can use FOR XML PATH('')
in an inner query to get the concatenated values and then use it to match with CourseEventKey
from the outer query:
;WITH CTE
AS
(
SELECT DISTINCT
ces.CourseEventKey,
up.Firstname + ' ' + up.Lastname AS Name
FROM InstructorCourseEventSchedule ices
INNER JOIN CourseEventSchedule ces ON ces.CourseEventScheduleKey = ices.MemberKey
INNER JOIN UserProfile up ON up.UserKey = ices.UserKey
WHERE ces.CourseEventKey IN
(
SELECT CourseEventKey
FROM @CourseEvents
)
)
SELECT DISTINCT i1.CourseEventKey,
STUFF(
(SELECT
'; ' + Name
FROM CTE i2
WHERE i1.CourseEventKey = i2.CourseEventKey
FOR XML PATH(''))
,1,2, ''
)
FROM CTE i1
ORDER BY i1.CourseEventKey
Upvotes: 5