September 2, 2011 at 5:30 am
EmpCodeShiftDateTime
2058 01/02/2011 13:43
2058 01/02/2011 20:12
2058 01/03/2011 14:04
2058 01/03/2011 20:13
2058 01/04/2011 14:09
2058 01/04/2011 20:12
I Want Show Like This
EmpCode ShiftDateTime TimeDetail
2058 01/02/2011 13:43 13:43,2012
2058 01/03/2011 14:04 14:04,20:13
2058 01/04/2011 14:09 14:19,20:12
Please Help me
September 2, 2011 at 6:11 am
You need to pivot the results:
WITH SampleData
AS (
SELECT EmpCode,
CAST(ShiftDateTime AS DateTime) AS ShiftDateTime,
RN = ROW_NUMBER() OVER (PARTITION BY EmpCode, CAST(ShiftDateTime AS Date) ORDER BY ShiftDateTime)
FROM (VALUES
('2058', '01/02/2011 13:43')
,('2058', '01/02/2011 20:12')
,('2058', '01/03/2011 14:04')
,('2058', '01/03/2011 20:13')
,('2058', '01/04/2011 14:09')
,('2058', '01/04/2011 20:12')
) AS Data (EmpCode,ShiftDateTime)
)
SELECT EmpCode, ShiftDate, [1], [2]
FROM (
SELECT EmpCode,
CAST(ShiftDateTime AS Date) AS ShiftDate,
CONVERT(char(5), ShiftDateTime, 114) AS ShiftTime,
RN
FROM SampleData
) AS src
PIVOT( MIN(ShiftTime) FOR RN IN([1], [2]) ) AS pvt
Hope this helps
Gianluca
-- Gianluca Sartori
September 2, 2011 at 6:16 am
Please keep to one thread
http://www.sqlservercentral.com/Forums/Topic1169158-392-1.aspx
September 2, 2011 at 6:27 am
Thanks
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply