简体   繁体   中英

How do I Stored Proc this data with SQL?

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

In this query i have used Logtime field type varchar as a string and Date field as a DateTime so help me for changes in this query.

this query is perfect but i have used logtime as varchar type.so i dont get perfect output in my report.

how can achieve it ?

Edit: (after question clarification)

I would do it that way, example data:

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'

and final query:

;with cte as (
    select
        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
)
select c1.empid as EmployeeID, c1.logtime as InTime, c2.logtime as OutTime, 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 c2.row = (select max(row) from cte where empid = c2.empid) and p.empid = c2.empid
where c1.row % 2 = 1
order by c1.empid, c1.row

result:

EmployeeID  InTime  OutTime Punches
11          8:03    8:09    
11          9:37    9:44    
11          9:48    9:50    (8:03 In, 8:09 Out, 9:37 In, 9:44 Out, 9:48 In, 9:50 Out)
12          8:38    9:31    
12          9:31    9:36    (8:38 In, 9:31 Out, 9:31 In, 9:36 Out)

Added:

Another report:

;with cte as (
    select
        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
)
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

Please bear in mind that result is for mine sample data:

EmployeeID  InTime  OutTime Punches
11          8:03    9:50    (8:03 In, 8:09 Out, 9:37 In, 9:44 Out, 9:48 In, 9:50 Out)
12          8:38    9:36    (8:38 In, 9:31 Out, 9:31 In, 9:36 Out)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM