简体   繁体   中英

how to count total time in HH MM SS format

There is one table which is having useage detail in HH MM SS format for every user, how can i count the total usage in HH MM SS format

在此处输入图片说明


i want result to be total=6:47:33
i know it is pretty basic but unable to figure out

You can always do it the old-fashioned way:

;WITH s AS (SELECT SUM(((Hours * 60) + Minutes) * 60 + Seconds) AS t FROM myTable)
SELECT CAST(t / 60 / 60 AS VARCHAR(10)) + ':' + RIGHT('0' + CAST((t / 60) % 60 AS VARCHAR(2)),2) + ':' + RIGHT('0' + CAST(t % 60 AS VARCHAR(2)), 2) AS total
FROM s

SQL Fiddle example

;with cte as (select (hours * (60*60))+(minutes * 60)+seconds as seconds from table)

select cast(sum(case when seconds = 0 then 0 else 1. / (86400. / seconds) end) as datetime)
from cte

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM