现在有一个上班记录表,假定有如下记录:
人员  日期         起始时间   结束时间
张三  2004-05-08  08:00:00 11:00:00
张三  2004-05-08  13:00:00 17:00:00
李四  2004-05-09  08:00:00 11:00:00
李四  2004-05-09  13:00:00 17:00:00
王五  2004-05-10  08:00:00 11:00:00
王五  2004-05-10  13:00:00 17:00:00这些记录是从一个表中提取出来的,并且记录内容不定,记录的数目也不定。
还有另外一个请假记录表,记录如下:
人员  日期        起始时间  结束时间
张三  2004-05-08  08:00:00  17:00:00
李四  2004-05-09  10:00:00  16:00:00
王五  2004-05-10  09:00:00  10:00:00我想计算出张三2004-05-08这天请假用了多长时间(精确到分钟),李四2004-05-09这天请假用了多长时间(精确到分钟)。可以一次只算一个人的,最好是同时计算出这些人的请假时间。注意请假记录表中中间有休息时间。

解决方案 »

  1.   

    时间函数,在这里找吧:×那怎得到当前的年、月、日?
    Delphi中自带 DecodeDate
    uses DateUtils
    它的申明为: procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word);使用样例:var
      d:TDateTime;
      year,month,day:Word;
    begin
      DecodeDate(d,year,month,day);
    end;//==================================================================
    *显示出时间
    FormatDateTime('yyyy"年"m"月"d"日 "dddd '+ 'hh:mm:ss AM/PM', Now);
    Year := FormatDateTime('YYYY',Date);
    Month := FormatDateTime('MM',Date);
    Day := FormatDateTime('DD',Date);//==================================================================
    *TDateTime  TDate 类Now 返回当前日期及时间 
    Date 返回当前日期 
    Time 返回当前时间 
    DateTimeToStr 按缺省格式将日期和时间值转换为字符串;特定格式转换可用 FormatDateTime函数 
    DateTimeToString 按缺省格式将日期和时间值拷贝到字符串缓冲区 
    DateToStr 将TDateTime值的日期部分转为字符串 
    TimeToStr 将TDateTime值的时间部分转为字符串 
    FormatDateTime 按特定格式将日期和时间值转换为字符串 
    StrToDateTime 将带有日期和时间信息的字符串转换为TdateTime类型值,如串有误将引发一个异常 
    StrToDate 将带有日期信息的字符串转换为TDateTime类型格式 
    StrToTime 将带有时间信息的字符串转换为TDateTime类型格式 
    DayOfWeek 根据传递的日期参数计算该日期是一星期中的第几天 
    DecodeDate 根据日期值返回年、月、日值 
    DecodeTime 根据时间值返回时、分、秒、毫秒值 
    EncodeDate 组合年、月、日值为TDateTime类型值 
    EncodeTime 组合时、分、秒、毫秒值为TDateTime类型值 
    //=============================================================
    在一个确定时间上增加月数
    function IncMonth(const Date: TDateTime; NumberOfMonths: Integer = 1): TDateTime;
      

  2.   

    select datediff(mi,起始时间-结束时间)-休息的分钟 as 请假时间 from 请假表 
    where ...
      

  3.   

    我觉得用SQL语句无法计算,只能通过算法实现了,不知道各位高手有没有比较好的算法?
      

  4.   

    用数组计算。调用这个函数即可
    function GetValue:Real;
    const  Time:array[0..5] of integer=(1,2,3,4,5,6);
    var
      a,b,Value,aTmp,bTmp:Real;
      i,j,k:Integer;  function GetNextIndex(value:Real;Arr:array of Integer):Integer;
      var
        i:Integer;
      begin
          for i:=0 to high(arr) do
          begin
            if arr[i]>Value then
            begin
                Result:=i;
                Break;
            end;
          end;
      end;
    begin
      a:=1.5;
      b:=2.5;  if GetNextIndex(a,Time)=GetNextIndex(b,Time) then
        if (GetNextIndex(a,Time) mod 2=0)   then
        begin
            Result:=0;
            Exit;
        end
        else
        begin
            Result:=b-a;
            Exit;
        end;
      Result:=b-a;
      
      if (GetNextIndex(a,Time) mod 2)=1 then
        aTmp:=0
      else
        aTmp:=Time[GetNextIndex(a,Time)]-a;
      if (GetNextIndex(b,Time) mod 2)=1 then
        bTmp:=0
      else
        bTmp:=b-Time[GetNextIndex(b,Time)-1];
      i:=GetNextIndex(a,Time);
      j:=GetNextIndex(b,Time)-1;
      Value:=0;
      while i<j do
      begin
          if (i mod 2)=1 then
            Value:=Value+Time[i+1]-Time[i];
          inc(i);
      end;
      Result:=Result-aTmp-bTmp-Value;
    end;