怎么做两个不同时间的总和

时间:2015-03-05 07:05:17

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

我有以下代码:

SELECT distinct userinfo.userid,userinfo.name,timeframe,deptname from EarlyOut
INNER JOIN userinfo ON USERINFO.USERID = earlyout.USERID
INNDER JOIN DEPARTMENTS ON DEPARTMENTS.DEPTID = EarlyOut.DEFAULTDEPTID
 where date>='2015-02-01' and date<='2015-02-28' and
DEPARTMENTS.DEPTNAME = 'abc'

现在从上面的代码我得到以下答案:

userid  name    timeframe             deptname
111     xyz     2015-02-05 08:00:00    abc 
111     xyz     2015-02-10 09:15:00    abc

现在我想要following输出:

userid  name    timeframe             deptname
111     xyz     17:15:00                abc 

我想要总时间

那我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

试试这个:

SELECT DISTINCT userinfo.userid,
                userinfo.name,
                Dateadd(ms, Sum(Datediff(ms, '00:00:00.000', timeframe)), '00:00:00.000') AS newtimeframe,
                deptname
FROM   EarlyOut
       INNER JOIN userinfo
               ON USERINFO.USERID = earlyout.USERID
       INNER JOIN DEPARTMENTS
               ON DEPARTMENTS.DEPTID = EarlyOut.DEFAULTDEPTID
WHERE  date >= '2015-02-01'
       AND date <= '2015-02-28'
       AND DEPARTMENTS.DEPTNAME = 'abc'
GROUP  BY userinfo.userid,
          userinfo.name,
          DEPARTMENTS.DEPTNAME