如何从hh:mm:ss中的Time数据类型列中加总时间?

时间:2015-01-26 23:19:28

标签: sql sql-server time sum

我有一个包含4列(TIME(0)数据类型)的SQL Server表。

每行对应一天,所以我有:

Col Time 1 -------- Col Time 2 ------ Col Time 3

'03:23:12' -------- '21:33:04' ------ '07:44:11'
'21:14:45' -------- '12:43:44' ------ '01:56:43'
'15:22:36' -------- '19:33:55' ------ '04:03:11'
'08:21:46' -------- '01:23:14' ------ '06:09:11'

我想SUM每列,以便我可以获得总小时,分钟和秒hh:mm:ss)。

因此,例如Col Time 1我应该:

48:54:46

2 个答案:

答案 0 :(得分:4)

首先,您的最终值应为48:22:19而不是48:54:46
其次,您无法使用TIME类型来接收所需的结果,因为hh元素范围是两位数,范围从0 23 ,表示小时(More information

这是一种解决方法,首先您SUM TIME列转换中的每个值,然后单独找到小时,分钟和秒

SELECT CAST(t.time_sum/3600 AS VARCHAR(2)) + ':'
     + CAST(t.time_sum%3600/60 AS VARCHAR(2)) + ':'
     + CAST(((t.time_sum%3600)%60) AS VARCHAR(2))
FROM ( SELECT SUM(DATEDIFF(S, '00:00:00', col1)) AS time_sum
       FROM tbl) t

SQLFiddle

答案 1 :(得分:-2)

我找到了更简单的方法。

var skillArray = [
{ID: 1, name: "الغطس"},
{ID: 2, name: "الحريق"},
{ID: 3, name: "المعالجة"},
{ID: 4, name: "الميكانيكا"},
{ID: 5, name: "السيول"},
{ID: 6, name: "التغريز"},
{ID: 7, name: "الكهرباء"},
{ID: 8, name: "الاحتفالات"},
{ID: 9, name: "المناسبات"},
{ID: 10, name: "الصلاة"}
]

var splitstr = ["9", "7"]

const removeSkills = (source, removeList) => {
  return source.reduce((prev, curr) => {
    if(!removeList.includes(curr.ID.toString()) ){
      return prev.concat(curr)
    }
    return prev
  }, [])
}
const result = removeSkills(skillArray, splitstr)
console.log(result)