Данные моей таблицы выглядят так:
LogId EmployeeId LogTime Date
31 22 09:59:00 AM 2011-11-04 00:00:00.000
40 22 12:15:00 PM 2011-11-04 00:00:00.000
43 22 13:15:00 PM 2011-11-04 00:00:00.000
45 22 18:16:00 PM 2011-11-04 00:00:00.000
47 22 18:40:00 PM 2011-11-04 00:00:00.000
Мне нужны данные этого типа в моем отчете:
SNo. EmpID In Duration Out Duration Punch Records
1 22 09:59 18:16 (09:59 in, 12:15 out, 13:15 in, 18:16 out)
------- Ответ на запрос ----------
if object_id('tempdb..#timing') is not null drop table #timing
create table #timing (
logid int identity(1, 1),
empid int,
logtime datetime
)
insert into #timing
select 11, '20111201 8:03' union all select 11, '20111201 8:09' union all
select 12, '20111201 8:38' union all select 12, '20111201 9:31' union all
select 12, '20111201 9:31' union all select 12, '20111201 9:36' union all
select 11, '20111201 9:37' union all select 11, '20111201 9:44' union all
select 11, '20111201 9:48' union all select 11, '20111201 9:50'
;with cte as (
select top 100 percent
empid,
cast(datepart(hh, logtime) as varchar(2)) + ':' +
right('0' + cast(datepart(mi, logtime) as varchar(2)), 2) as logtime,
row_number() over (partition by empid order by logid) as row
from #timing
order by logid asc
)
select c1.empid as EmployeeID, min(c1.logtime) as InTime, max(c2.logtime) as OutTime, min(isnull(p.punches, '')) as Punches
from cte c1
join cte c2 on c1.row + 1 = c2.row and c1.empid = c2.empid
left join (
select empid,
(select '(' + stuff(
(select ', ' +c1.logtime + ' In, ' + c2.logtime + ' Out'
from cte c1
join cte c2 on c1.row + 1 = c2.row and c1.empid = c2.empid
where c1.row % 2 = 1 and c3.empid = c1.empid
for xml path(''), type).value('text()[1]','varchar(max)'), 1, 2, '') + ')'
) as punches
from cte c3
group by empid
) as p on p.empid = c1.empid
group by c1.empid
order by c1.empid
В этом запросе я использовал тип поля Logtime varchar в качестве строки и поле даты в качестве DateTime, поэтому помогите мне с изменениями в этом запросе.
этот запрос идеален, но я использовал logtime как varchar type.so я не получаю идеальный вывод в моем отчете.
как этого достичь?