Columns to Rows in Sql Server with multiple data

前端 未结 1 735
星月不相逢
星月不相逢 2021-01-23 17:11

please help.. please see example data below and the desired output..

Data:

EmployeeID    Date        In          Out
00001         01/01/2018 | 07:00 AM          


        
1条回答
  •  [愿得一人]
    2021-01-23 17:35

    Use PIVOT:

    WITH Src AS
    (
      SELECT * FROM (VALUES 
      ('00001' ,'01/01/2018', '07:00 AM', '05:00 PM'),
      ('00001' ,'01/02/2018', '07:00 AM', '05:00 PM'),
      ('00001' ,'01/03/2018', '07:00 AM', '05:00 PM'),
      ('00002' ,'01/01/2018', '07:00 AM', '05:00 PM'),
      ('00002' ,'01/02/2018', '07:00 AM', '05:00 PM'),
      ('00002' ,'01/03/2018', '07:00 AM', '05:00 PM')) T(EmployeeID,[Date],[In],[Out])
    )
    SELECT * FROM
    (SELECT EmployeeID, [Date], [In]+' - '+[Out] [In] FROM Src) T
    PIVOT (MAX([In]) FOR [Date] IN ([01/01/2018],[01/02/2018],[01/03/2018])) AS P
    

    0 讨论(0)
提交回复
热议问题