1

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

Data:

EmployeeID    Date        In          Out
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

Desired Output

EmployeeID    01/01/2018         |  01/02/2018         |  01/03/2018
00001         07:00 AM - 05:00PM |  07:00 AM - 05:00PM |  07:00 AM - 05:00PM
00002         07:00 AM - 05:00PM |  07:00 AM - 05:00PM |  07:00 AM - 05:00PM
5
  • you can find many solution in SO. Keyword is pivot. If you know already about it. then what you tried yet..? Commented Sep 11, 2018 at 9:58
  • I've tried but i didn't get desired output Commented Sep 12, 2018 at 1:06
  • Next time upload what you tried.. Commented Sep 12, 2018 at 6:47
  • noted on this sir Commented Sep 12, 2018 at 7:00
  • Thx..im not sir I am just 23 Commented Sep 12, 2018 at 7:04

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

how can i dynamically call something like this sir.. declare @xxx nvarchar(200) = '01/01/2018' select @xxx from pivot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.