如何在SQL Server 2014中获得总时间

时间:2015-11-29 12:56:36

标签: sql-server

我有3列数据类型time

如何使用SQL查询获取每个人的总时间?

我尝试了以下我得到结果格式90,30,120分钟我希望结果是HH:mm

01:30, 00:30, 02:00

代码:

SELECT 
    EmpNum, 
    SUM(DATEDIFF(MINUTE, '0:00:00', EarlyOT)),
    SUM(DATEDIFF(MINUTE, '0:00:00', late)), 
    SUM(DATEDIFF(MINUTE, '0:00:00', [ExcessOT])) 
FROM 
    TblAttendReports 
WHERE
    EmpNum = 11983
GROUP BY 
    EmpNum

1 个答案:

答案 0 :(得分:1)

您只需要重新格式化输出。这是一个想法:

;WITH TimesCte as
(
    SELECT 
        EmpNum, 
        SUM(DATEDIFF(MINUTE, '0:00:00', EarlyOT)) ear_min,
        SUM(DATEDIFF(MINUTE, '0:00:00', late)) lat_min, 
        SUM(DATEDIFF(MINUTE, '0:00:00', [ExcessOT])) exc_min 
    FROM 
        TblAttendReports 
    WHERE
        EmpNum = 11983
    GROUP BY 
        EmpNum
)
SELECT  EmpNum,
        ,RTRIM(ear_min/60) + ':' + RIGHT('0' + RTRIM(ear_min%60),2)
        ,RTRIM(lat_min/60) + ':' + RIGHT('0' + RTRIM(lat_min%60),2)
        ,RTRIM(exc_min/60) + ':' + RIGHT('0' + RTRIM(exc_min%60),2)
  FROM TimesCte;