2017-02-24 237 views
1

在此任一幫喜歡能「希望2行也在第一行」 ..SQL Server的內部連接

SELECT BDSReleaseBDDSBPD.ProjectID, 
     BDSFlowDetails.Flowid,IterationID, 
     case when ReleaseTask = 'BDDS' then UID end as 'BDDS UID' , 
     case when ReleaseTask = 'BDDS' then Concat(FirstName,'_',Lastname) end as BDDSReleasedBy, 
     case when ReleaseTask = 'BDDS' then ReleaseDate end as 'BDDS Released Date' , 
     case when ReleaseTask = 'BPD' then UID end as 'BPD UID' , 
     case when ReleaseTask = 'BPD' then Concat(FirstName,'_',Lastname) end as BPDReleasedBy, 
     case when ReleaseTask = 'BPD' then ReleaseDate end as 'BPD Released Date' 
FROM BDSReleaseBDDSBPD 
inner join BDSFlowDetails on BDSFlowDetails.FlowID = BDSReleaseBDDSBPD.FlowID 
inner join BDSUserProfile on BDSUserProfile.UserName=BDSReleaseBDDSBPD.UserName 
WHERE BDSReleaseBDDSBPD.ProjectID = 861 
order by BDSFlowDetails.Flowid 

[SQL結果集..] [![請在此輸入圖片說明] [1] [1]

enter image description here

回答

1

可以轉動的ProjectIDFlowidIterationID列,使用聚合到記錄組合:

SELECT BDSReleaseBDDSBPD.ProjectID, 
     BDSFlowDetails.Flowid, 
     IterationID, 
     MAX(CASE WHEN ReleaseTask = 'BDDS' THEN UID END) AS 'BDDS UID', 
     MAX(CASE WHEN ReleaseTask = 'BDDS' 
       THEN CONCAT(FirstName, '_', Lastname) END) AS BDDSReleasedBy, 
     MAX(CASE WHEN ReleaseTask = 'BDDS' 
       THEN ReleaseDate END) AS 'BDDS Released Date' , 
     MAX(CASE WHEN ReleaseTask = 'BPD' THEN UID END) AS 'BPD UID' , 
     MAX(CASE WHEN ReleaseTask = 'BPD' 
       THEN CONCAT(FirstName, '_', Lastname) END) AS BPDReleasedBy, 
     MAX(CASE WHEN ReleaseTask = 'BPD' 
       THEN ReleaseDate END) AS 'BPD Released Date' 
FROM BDSReleaseBDDSBPD 
INNER JOIN BDSFlowDetails 
    ON BDSFlowDetails.FlowID = BDSReleaseBDDSBPD.FlowID 
INNER JOIN BDSUserProfile 
    ON BDSUserProfile.UserName = BDSReleaseBDDSBPD.UserName 
WHERE BDSReleaseBDDSBPD.ProjectID = 861 
GROUP BY BDSReleaseBDDSBPD.ProjectID, 
     BDSFlowDetails.Flowid, 
     IterationID 
ORDER BY BDSFlowDetails.Flowid