September 2, 2011 at 5:44 am
EmpCode ShiftDateTime
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,20:12
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 5:53 am
What version of SQL you using? If 2008 you can use the TIME data type:
http://msdn.microsoft.com/en-us/library/bb677243.aspx
September 2, 2011 at 6:14 am
Use the rownumber function to create a unique row number for the starting / ending times.
Then use pivot functionality , i would prefer a manual pivot as shown here http://www.sqlservercentral.com/articles/T-SQL/73887/ over PIVOT , but the choice is yours, to bring the two rows into one.
September 2, 2011 at 11:11 am
Probably should this kind of formatting in the front end, but if you must do this is sql, maybe this will help:SELECT
EmpCode,
MIN(ShiftDateTime),
CAST(CAST(MIN(ShiftDateTime) AS TIME) AS CHAR(5))
+ ','
+ CAST(CAST(MAX(ShiftDateTime) AS TIME) AS CHAR(5))
FROM
TableName
GROUP BY
EmpCode,
CAST(ShiftDateTime AS DATE)Mis-read teh requirement before, but I think this edit will work.
September 2, 2011 at 11:18 am
Like Dave suggested, use row_number(), then add a little math, a crosstab and some string manipulation...
declare @tbl table (
EmpCode int not null,
ShiftDateTime datetime not null
);
insert @tbl(EmpCode,ShiftDateTime)
select 2058, '01/02/2011 13:43'
union all select 2058, '01/02/2011 20:12'
union all select 2058, '01/03/2011 14:04'
union all select 2058, '01/03/2011 20:13'
union all select 2058, '01/04/2011 14:09'
union all select 2058, '01/04/2011 20:12';
with cte as (
select -1 + row_number() over (
partition by
EmpCode
order by t.ShiftDateTime) as Nr,
t.EmpCode,
t.ShiftDateTime
from @tbl t
)
select Shifts.EmpCode,
Shifts.StartTime as ShiftDateTime,
right( '0' + convert(varchar(2), datepart(hour, Shifts.StartTime)), 2) + ':' + right( '0' + convert(varchar(2), datepart(minute, Shifts.StartTime)), 2) +
',' + right( '0' + convert(varchar(2), datepart(hour, Shifts.EndTime)), 2) + ':' + right( '0' + convert(varchar(2), datepart(minute, Shifts.EndTime)), 2)
from (
select EmpCode,
t.Nr / 2 as ShiftNr,
max(case t.Nr % 2 when 0 then t.ShiftDateTime end) as StartTime,
max(case t.Nr % 2 when 1 then t.ShiftDateTime end) as EndTime
from cte t
group by t.EmpCode,
t.Nr / 2
) Shifts
September 2, 2011 at 8:11 pm
tamil.selvanmca (9/2/2011)
EmpCode ShiftDateTime2058 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,20:12
2058 01/03/2011 14:04 14:04,20:13
2058 01/04/2011 14:09 14:19,20:12
Please Help me
What do you want to do for someone who works past midnight into the next day?
Also, how are you guaranteeing that the pairs of times always start which a "Start" and are followed by an "End"?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply