【问题标题】:how to count total time in HH MM SS format如何以 HH MM SS 格式计算总时间
【发布时间】:2012-10-29 10:21:12
【问题描述】:

有一个表格,每个用户都有HH MM SS 格式的使用详细信息,我如何计算HH MM SS 格式的总使用情况


i want result to be total=6:47:33
我知道这是非常基本的,但无法弄清楚

【问题讨论】:

    标签: sql-server time-format


    【解决方案1】:

    你总是可以用老式的方式来做:

    ;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

    【讨论】:

    • 我想你可以用 TIMEDATEADD 做点什么,但感觉很难看,因为我认为你不能快速 SUM 行。
    【解决方案2】:
    ;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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 2013-12-10
      相关资源
      最近更新 更多