在PIVOT中组合列文本

时间:2013-07-11 16:28:33

标签: sql-server sql-server-2008 tsql

我有一组目前看起来像这样的数据:current data format

出于报告目的,我必须透视InterviewNotes列。就我而言:

SELECT CandidateId, JobId, [1] AS Int1, [2] AS Int2, [3] AS Int3
FROM (SELECT CandidateId, JobId, InterviewNotes, InterviewNo FROM dbo.FactInterview) AS sourceTable
PIVOT (
    MAX(InterviewNotes) FOR InterviewNo IN ([1],[2],[3])
) AS PivotTable

对于每一列,我想显示:InterviewName,InterviewDt,InterviewNotes连接。如:

Int1: InterviewerName - InterviewDt - InterviewNotes
Int2: InterviewerName - InterviewDt - InterviewNotes

1 个答案:

答案 0 :(得分:1)

如果您想将InterviewerNameInterviewDtInterviewNotes连接在一起显示在最后一列中,请在PIVOT之前连接值:

SELECT CandidateId, JobId, [1] AS Int1, [2] AS Int2, [3] AS Int3
FROM 
(
    SELECT CandidateId, JobId, InterviewNo, 
        InterviewNotes = InterviewerName + ' - '+ convert(varchar(10), InterviewDt, 120) +' - '+ InterviewNotes
    FROM dbo.FactInterview
) AS sourceTable
PIVOT 
(
    MAX(InterviewNotes) FOR InterviewNo IN ([1],[2],[3])
) AS PivotTable