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

问题描述 投票:1回答:3

我有这个问题:

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

我在工作解决方案中遇到过像我这样的问题,但我不能为我的生活调整这些解决方案来处理我的数据。

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

提前致谢。

sql sql-server tsql concatenation
3个回答
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

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

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
                )
© www.soinside.com 2019 - 2024. All rights reserved.