How can I get the sum of multiple datetime values?

后端 未结 2 439
执笔经年
执笔经年 2020-11-29 12:27

I have a query such that the query\'s result is:

SELECT CONVERT(VARCHAR(8),(MAX(END_TIME)-MIN(START_TIME)),108) as DURATION WHERE ... GROUP BY TITLE
<         


        
2条回答
  •  心在旅途
    2020-11-29 12:45

    Convert minutes into seconds

    SUM() the seconds

    Convert back to minutes


    The following will give you the SUM of seconds:

    SET @Seconds = SELECT SUM(DATEDIFF(SECOND, [START_TIME], [END_TIME]))
    

    The following then turns that into a datetime object:

    select convert(varchar(8), dateadd(second, @Seconds, 0),  108)
    

    Or as 1 query:

    SELECT convert(varchar(8), dateadd(second, SUM(DATEDIFF(SECOND, [START_TIME], [END_TIME])), 0),  108)
    

提交回复
热议问题