Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何使用SQL存储Proc此数据?_.net_Sql Server 2005_Reporting Services - Fatal编程技术网

.net 如何使用SQL存储Proc此数据?

.net 如何使用SQL存储Proc此数据?,.net,sql-server-2005,reporting-services,.net,Sql Server 2005,Reporting Services,在这个查询中,我使用Logtime字段类型varchar作为字符串,Date字段作为DateTime,所以请帮助我了解这个查询中的更改 这个查询很完美,但是我使用了logtime作为varchar类型。所以我在报告中没有得到完美的输出 怎样才能做到呢 编辑:(问题澄清后) 我会这样做,例如数据: if object_id('tempdb..#timing') is not null drop table #timing create table #timing ( logid int

在这个查询中,我使用Logtime字段类型varchar作为字符串,Date字段作为DateTime,所以请帮助我了解这个查询中的更改

这个查询很完美,但是我使用了logtime作为varchar类型。所以我在报告中没有得到完美的输出

怎样才能做到呢

编辑:(问题澄清后)

我会这样做,例如数据:

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
;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
最后一个问题:

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
        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
添加:

另一份报告:

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)
请记住,结果适用于矿山样本数据:

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
;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

您的计时表是什么样子的?我的表数据如下:LogId EmployeeId LogTime date如何确定要输出“punchs”的行to?LogTime行是确定打孔时间…我如何在此查询中添加EmployeeID?我还想添加EmployeeID。@user1070790为您的另一份报告添加了查询。不客气,我希望您不会忘记up vote并接受:-)。请检查我的编辑。我需要一些有关此查询的帮助…@Michal Powaga我如何将此查询用于存储过程?