将结果合并到一个单元格中

时间:2017-09-25 12:52:29

标签: sql sql-server-2014

我正在尝试从我们的管理信息系统数据库中提取一份员工清单,其中包括他们的名字,姓氏,角色和科目。

以下SQL查询:

Select TblStaff.Firstname, TblStaff.Surname, txtSchoolRolesName
from 
TblStaff 
LEFT JOIN TblStaffManagementSchoolRoles ON TblStaff.TblStaffID = TblStaffManagementSchoolRoles.intStaff
LEFT JOIN TblStaffManagementSchoolRolesObjects ON TblStaffManagementSchoolRoles.intSchoolRole = TblStaffManagementSchoolRolesObjects.TblStaffManagementSchoolRolesObjectsID
WHERE TblStaff.SystemStatus = 1

返回以下内容:

Current output

但我需要它看起来像这样:

Required Format

最好这样做?

1 个答案:

答案 0 :(得分:0)

您可以使用Stuff()将值连接到单个单元格

以下是一个例子:

create table #temp (
firstname varchar(255),
surname varchar(255),
[role] varchar(255),
[subject] varchar(255)

)

insert into #temp
values ('Jane', 'Smith', 'Maths Teacher', 'Math'),
 ('Jane', 'Smith', 'Maths Teacher', 'Physics'),
 ('Jane', 'Smith', 'Maths Teacher', 'Tutorial'),
 ('Jane', 'Smith', 'Physics Teacher', 'Math'),
 ('Jane', 'Smith', 'Physics Teacher', 'Physics'),
 ('Jane', 'Smith', 'Physics Teacher', 'Tutorial'),
 ('Kate', 'Smith', 'Maths Teacher', 'Math1'),
 ('Kate', 'Smith', 'Maths Teacher', 'Physics'),
 ('Kate', 'Smith', 'Maths Teacher', 'Tutoria'),
 ('Kate', 'Smith', 'Physics Teacher', 'Math'),
 ('Kate', 'Smith', 'Physics Teacher', 'Physics'),
 ('Kate', 'Smith', 'Physics Teacher', 'Tutorial')

select * from #temp


select distinct firstname,surname, STUFF( (SELECT distinct ',' + [role] 
                             FROM [#temp] t1
                             where t1.firstname = t2.firstname and t1.surname = t2.surname
                             FOR XML PATH('')), 
                            1, 1, ''),

            STUFF( (SELECT distinct ',' + [subject] 
                FROM #temp t1
                 where t1.firstname = t2.firstname and t1.surname = t2.surname                   
                FOR XML PATH('')), 
            1, 1, '') as [subject]
from #temp t2

drop table #temp

使用Cross APPLY

可以实现另一种方式
create table #temp (
firstname varchar(255),
surname varchar(255),
[role] varchar(255),
[subject] varchar(255)

)

insert into #temp
values ('Jane', 'Smith', 'Maths Teacher', 'Math'),
    ('Jane', 'Smith', 'Maths Teacher', 'Physics'),
    ('Jane', 'Smith', 'Maths Teacher', 'Tutorial'),
    ('Jane', 'Smith', 'Physics Teacher', 'Math'),
    ('Jane', 'Smith', 'Physics Teacher', 'Physics'),
    ('Jane', 'Smith', 'Physics Teacher', 'Tutorial'),
    ('Kate', 'Smith', 'Maths Teacher', 'Math1'),
    ('Kate', 'Smith', 'Maths Teacher', 'Physics'),
    ('Kate', 'Smith', 'Maths Teacher', 'Tutoria'),
    ('Kate', 'Smith', 'Physics Teacher', 'Math'),
    ('Kate', 'Smith', 'Physics Teacher', 'Physics'),
    ('Kate', 'Smith', 'Physics Teacher', 'Tutorial')


select distinct firstname,surname,rol.[role], sub.subject
from #temp t2
CROSS APPLY (SELECT convert(varchar(20), [role]) + ','
                            FROM #temp t1
                            where t1.firstname = t2.firstname and t1.surname = t2.surname                                  
                            GROUP BY firstname,surname,[role]
                                    FOR XML PATH('')) rol([role])
CROSS APPLY (SELECT    convert(varchar(20), [subject]) +',' 
                    FROM #temp t1
                    where t1.firstname = t2.firstname and t1.surname = t2.surname                                  
                    GROUP BY  firstname,surname,[subject]
                            FOR XML PATH('')) sub([subject])   


drop table #temp