我有列(时间数据类型),其中包含类似的值
9:25:03
4:43:21
2:50:13
11:25:33
,我应该写什么查询来获取列中所有值的总和
试试这个:
;with cte as(
select SUM(DATEPART(hh,tm))*3600+
SUM(DATEPART(MI,tm))*60+
SUM(DATEPART(SS,tm)) tme
from time_table)
SELECT cast(datepart(hh,cast(CONVERT(varchar(8),
DATEADD(ss, tme , 0), 114) as time))+
datediff(dd,'1900-01-01',CONVERT(varchar,
DATEADD(ss, tme , 0), 121))*24 as varchar(20))+
right(CONVERT(varchar(8), DATEADD(ss, tme , 0), 114),6)
from cte
结果
28:24:10
把你的时间变成秒,然后把它们加起来。
with YourTable(TimeCol) as
(
select '9:25:03' union all
select '4:43:21' union all
select '2:50:13' union all
select '11:25:33'
)
select sum(datediff(second, 0, TimeCol)) sumSeconds
from YourTable
结果:
sumSeconds
-----------
102250
试试这个:
create table tbl123(tm time)
insert into tbl123
values('9:25:03'),('4:43:21'),('2:50:13'),('11:25:33')
;WITH CTE as(select SUM(DATEPART(hh,tm)) as hh,SUM(DATEPART(mi,tm)) as mm,SUM(DATEPART(ss,tm)) as ss from tbl123)
SELECT cast(hh+(mm+(ss/60))/60 as varchar(10))+':'+
cast((((mm+(ss/60))%60)) as varchar(10))+':'+
cast(ss%60 as varchar(10))
from CTE
结果:
28:24:10