2017-09-25 41 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 

返回如下:

Current output

但我需要它看起來像這樣:

Required Format

將如何最好做到這一點?

+2

請編輯您的問題進行achive添加[** sample data **](http://plaintexttools.github.io/plain-text-table/)以及基於該數據的預期輸出。將它們提供爲[**格式化文本**](http://stackoverflow.com/help/formatting)並嚴格執行[**無屏幕截圖**](http://meta.stackoverflow.com/questions/285551/爲什麼-MAY-I-沒有上傳圖像-的代碼上那麼當灰化-A-問題/ 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 
相關問題