表结构:
工号   日期    刷卡时间
1001  2011-01-01   8:00
1001  2011-01-01   12:00
1001  2011-01-01   13:30
1001  2011-01-01   17:30
1001  2011-01-01   19:00
1001  2011-01-01   21:30
想得到的结果:
工号    日期    工作时间
1001 2011-01-01   10.5(4+4+2.5) 

解决方案 »

  1.   

    create table #temp
    (
    工号 varchar(10),
    日期 varchar(10),
    刷卡时间 varchar(10)
    )
    insert #temp
    select '1001', '2011-01-01', '8:00' union all
    select '1001', '2011-01-01', '12:00' union all
    select '1001', '2011-01-01', '13:30' union all
    select '1001', '2011-01-01', '17:30' union all
    select '1001', '2011-01-01', '19:00' union all
    select '1001', '2011-01-01', '21:30'
    go
    --SQL:
    ;with cte as
    (
    SELECT rowno=ROW_NUMBER() over(partition by 工号,日期 order by 刷卡时间), * FROM #temp
    )
    select 工号, 日期, 工作时间 = cast(1.*工作时间/60 + (工作时间%60)/60 as float) from
    (
    select a.工号, a.日期, 工作时间=SUM(DATEDIFF(minute, b.刷卡时间, a.刷卡时间))
    from cte a, cte b
    where a.rowno%2 = 1 
    and a.工号 = b.工号
    and a.日期 = b.日期
    and a.rowno = b.rowno-1
    group by a.工号, a.日期
    ) t
    /*
    工号 日期 工作时间
    1001 2011-01-01 10.5
    */
      

  2.   


    --建表
    create table #print(autoid int identity(1,1),userid varchar(10),date varchar(10), ptime varchar(10))insert into #print
    select '1001','2011-01-01','8:00'
    union all select '1001','2011-01-01','12:00'
    union all select '1001','2011-01-01','13:00'
    union all select '1001','2011-01-01','17:00'
    union all select '1001','2011-01-01','19:00'
    union all select '1001','2011-01-01','21:30'--临时表
    select userid,date, 
    cast(
    datediff(minute,min(cast(date+' '+ptime as datetime)),max(cast(date+' '+ptime as datetime)))*1.0/60
    as float) as ptime into #temp
    from #print
    group by (autoid+1)/2,userid,date--结果
    select userid as [员工编号], date as [日期], cast(sum(ptime) as varchar)+'('+
    stuff(
    (select '+'+cast(ptime as varchar) from #temp where userid=a.userid and date=a.date for xml path('')),1,1,''
    )+')' as [工作时间]
    from #temp as a
    group by userid, date员工编号       日期         工作时间
    ---------- ---------- ------------------
    1001       2011-01-01 10.5(4+4+2.5)(1 行受影响)
      

  3.   

    忘了,DB 说的ACCESS 2007 。