多行到单行多列

时间:2016-10-21 16:47:20

标签: sql sql-server

这就是我的数据:

## ID ## ## Phase ## ## Phase Datet ## ## Phase Temp ## ## TempRecorded ##
23300200    Induction   2016-10-07        30               2016-10-07
23300200    Maintenance 2016-10-07        35               2016-10-07
23300200    Rewarming   2016-10-07        35.5             2016-10-07  

我希望看起来像这样:

## ID ## ## Induction Phase ## ## Induction Phase Datet ## ## Induction Phase Temp ## ## Induction TempRecorded ## ## ID ## ## Maintenance Phase ## ## Maintenance Phase Datet ## ## Maintenance Phase Temp ## ## Maintenance TempRecorded ##
23300200    Induction                    2016-10-07                   30                     2016-10-07                          Maintenance                2016-10-07                  35                              2016-10-07 

1 个答案:

答案 0 :(得分:0)

我的同事为我提供了解决方案

SELECT I.*, M.*,R.*
FROM
(select * from #PhaseTemp where ValueText = 'Induction') I
LEFT JOIN (select * from #PhaseTemp where ValueText = 'Maintenance') M ON M.ClientGUID = I.ClientGUID
LEFT JOIN (select * from #PhaseTemp where ValueText = 'Rewarming') R ON R.CLIENTGUID = I.CLIENTGUID