添加等于24小时以上的时间值

时间:2014-10-02 15:45:06

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

我试图总结一些时间值,当我的总和超过24小时时,它会从总数中减去24个。

这是我的SQL:

SELECT 
    UserID,
    First_NM,
    Last_NM,
    MgrName,
    CONVERT(varchar(5),  DATEADD(minute, SUM(OnProd)/60, 0), 114) as OnProd,
    CONVERT(varchar(5),  DATEADD(minute, SUM(OffProd)/60, 0), 114) as OffProd, 
    CONVERT(varchar(5),  DATEADD(minute, SUM(OnProd+OffProd)/60, 0), 114) as SumProd    

FROM (
    SELECT 
        UserID,
        First_NM,
        Last_NM,
        MgrName,
        CASE WHEN WorkUnitType LIKE '%On%' THEN DATEDIFF(s, DateStart, DateEnd) ELSE 0 END AS OnProd,
        CASE WHEN WorkUnitType LIKE '%Off%' THEN DATEDIFF(s, DateStart, DateEnd) ELSE 0 END AS OffProd

    FROM dbo.vw_mos_DPL_Reporting

    ) a 
Group By 
    UserID,
    First_NM,
    Last_NM,
    MgrName

这是我目前的输出:

UserID         First_NM     Last_NM     OnProd  OffProd SumProd
A05878         STEVEN       LANTE       22:28   09:55   08:23
ARGLIN         ARNIE        FREEL       00:00   00:00   00:00
B96695         JENNIFER     LARK        21:32   18:49   16:21
PATYOI         PATTY        TROTTER     04:29   00:03   04:32

我该如何解决这个问题?为了清楚起见,我想展示像38:24这样的东西或者实际的总和。总小时数:总分钟数/ 60

1 个答案:

答案 0 :(得分:1)

它有点不合适,但您可以尝试分别计算分钟和小时数并连接,例如,而不是

CONVERT(varchar(5),  DATEADD(minute, SUM(OnProd)/60, 0), 114) as OnProd

SELECT CAST(SUM(OnProd)/3600 AS VARCHAR(6)) + ':' + RIGHT('00' + CAST((SUM(OnProd) % 3600) / 60 AS VARCHAR(6)),2) AS OnProd 

你也必须为OffProd和SumProd做同样的事情。