2017-09-25 2 views
-3

나는 경영 정보 시스템 데이터베이스에서 성, 성, 역할 및 학습 한 주제가 포함 된 직원 목록을 추출하려고합니다.결과를 하나의 셀로 병합

다음 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 

반환 다음

Required Format

방법은 다음과 같습니다

Current output

그러나 나는 다음과 같이해야합니다 이 일을하는 것이 가장 좋습니까?

+2

를 사용하여 achive 수있는 또 다른 방법 [** sample data **] (http://plaintexttools.github.io/plain-text-table/) 및 해당 데이터를 기반으로 예상되는 출력을 추가하십시오. 그것들을 [** 포맷 된 텍스트 **] (http://stackoverflow.com/help/formatting) 및 엄격하게 [** 스크린 샷 없음 **] (http://meta.stackoverflow.com/questions/285551/)으로 제공하십시오. 왜 코드를 업로드하지 않을 수 있습니까? - 질문을 할 때 이미지 이미지/285557 # 285557). ** 코멘트에 코드 또는 추가 정보를 게시하지 마십시오. [** 최소, 완전하고 검증 가능한 예제 **] (https://stackoverflow.com/help/mcve)를 가지고 있는지 확인하십시오. – SriniV

+2

사용중인 데이터베이스에 질문에 태그를 답니다. –

+0

그런 가치를 저장하는 것은 나쁜 생각 일뿐입니다. 또한 사용중인 데이터베이스를 설명하는 태그를 추가하십시오. – PacoDePaco

답변

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 
관련 문제