SUM小时和分钟SQL

时间:2016-09-06 04:36:56

标签: sql sql-server sql-server-2008

美好的一天,我试图将小时和分钟相加。但是现在我只能总结the hour part

这是我的查询。是的,当我将我的问题放在stackoverflow中时,我得到了这个查询

select 
    a.Nip, b.FullName, c.attendancedate, c.inTime, c.OutTime,
    DATEPART(wk, c.attendanceDate) week, 
    case 
       when DATEADD(HOUR, -8, OutTime) <= InTime then '00:00' 
       else CONVERT(VARCHAR(5),DATEADD(HOUR, -8, OutTime - InTime), 108) 
    end AS total  
from 
    DinasHoDetail a 
left join 
    Employee b on a.Nip = b.Nip
left join 
    DinasHoHeader d on a.KodeDinasHeader = d.KodeDinasHeader
left join 
    attendance c on a.Nip = c.Nip and attendancedate between d.startdate and d.enddate
        where a.KodeDinasHeader = 'DN0000001' and b.nip = '1602744'

从我的查询上面我得到了这个结果

enter image description here

所以,正如你从结果中看到的那样。有一个叫week的col。所以我试图按周计算每个total组。这是查询

select Nip,sum(ro) achieve,FullName 
from( 
    select a.Nip,b.FullName,DATEPART( wk, c.attendanceDate) week, 
sum(case when DATEADD(HOUR, -8, OutTime) <=InTime then 0 else DATEDIFF(HOUR, InTime, OutTime) - 8 end)/8 AS RO 
from DinasHoDetail a 
    left join Employee b on a.Nip = b.Nip 
    left join DinasHoHeader d on a.KodeDinasHeader = d.KodeDinasHeader 
    left join attendance c on a.Nip =c.Nip and attendancedate between d.startdate and d.enddate 
where a.KodeDinasHeader = 'DN0000001' and b.nip = '1602744'
    group by a.Nip,b.FullName,DATEPART( wk, c.attendanceDate)) q group by Nip ,FullName

这是结果

enter image description here

问题部分。

从第一个结果可以看出。第37周为08:11 (Eight hours eleven minute),第{37}周为38

当总数超过10:00 (Ten hours)时,每个人都会获得一项成就。所以我应该2对吗?

我相信我的问题是因为我不做08 hours (Group by week)

根据我上面的查询,我只能计算小时数而不是分钟数。所以我的问题是如何the datadd method for minute格式为DATEADD

2 个答案:

答案 0 :(得分:1)

您只需使用minute即可。试试下面这个脚本。

select Nip,sum(ro) achieve,FullName
     from( 
        select a.Nip,b.FullName,DATEPART( wk, c.attendanceDate) week, 
        sum(case 
            when DATEADD(MINUTE, -480, OutTime) <=InTime 
            then 0 
            else DATEDIFF(MINUTE, InTime, OutTime) - 480 end)/480 AS RO 
            from DinasHoDetail a 
                left join Employee b on a.Nip = b.Nip 
                left join DinasHoHeader d on a.KodeDinasHeader = d.KodeDinasHeader 
                left join attendance c on a.Nip =c.Nip and attendancedate 
                between d.startdate and d.enddate 
                where a.KodeDinasHeader = 'DN0000001' 
                group by a.Nip,b.FullName,DATEPART( wk, c.attendanceDate)
            ) q group by Nip ,FullName

答案 1 :(得分:0)

对于求和时间段并以新格式显示,不幸的是首先我们必须将时间差转换为最小有意义的时间单位。在您的情况下,我们必须以分钟为单位计算时差。 然后我们必须总结已经在几分钟内的所有时间段。 最后一步,我们将在几分钟内格式化所有这些数量;以新格式HH:MI或HH:MI:SS等。

请参阅教程Calculate Time Operations in SQL Server以获取示例案例

你会在那里看到一个函数fn_CalculateTimeInSeconds 您需要创建类似

的分钟
RETURN DATEPART(mi,@time) + 60 * DATEPART(hh,@time)

然后根据需要计算差异

你需要的最后一个函数类似于fn_CreateTimeFromSeconds,它会将计算出的时间(以分钟为单位)格式化为HH:MI格式

Create Function fn_CreateTimeFromMinutes (
 @minutes int
) Returns varchar(5)
BEGIN
Return
 --CAST (
  RIGHT('00' + CAST( (@minutes / 60) as varchar(2)), 2) + ':' +
  RIGHT('00' + CAST( (@minutes % 60) as varchar(2)), 2)
-- as Time)
END 
go