如何将具有重复ID的行中的不同列值连接成一行?

时间:2014-04-17 00:55:34

标签: sql sql-server tsql concatenation

我有这个问题:

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

它产生这个结果集:

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

数据准确无误,但我需要将结果集看起来像这样:

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

我已经看到过像我一样的问题,但是我不能在生活中使这些解决方案适应我的数据。

如何使用某种形式的连接来更改查询以生成第二个结果集?

提前致谢。

3 个答案:

答案 0 :(得分:4)

您可以在内部查询中使用FOR XML PATH('')来获取连接值,然后使用它与外部查询中的CourseEventKey匹配:

;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

答案 1 :(得分:0)

您创建一个函数,该函数作为CourseEventScheduleKey的参数并返回用户的连接字符串。然后你就可以使用它:

    select CourseEventScheduleKey, 
           dbo.getUsersForCourse(CourseEventScheduleKey) as Users
    from CourseEventSchedule 
    order by CourseEventScheduleKey

这应该返回你想要的东西。 该函数如下所示:

   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

答案 2 :(得分:0)

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
                )