nr time
726028xxx 00:07:07
735560css 00:07:37
726028xxx 00:07:55
答案 0 :(得分:5)
SELECT PHONE_NR, SUM(DATEPART(minute, TIME)) FROM [table] GROUP BY PHONE_NR;
据我所知,这应该适用于SQL Server DATETIME
和2008 TIME
数据类型。
示例:
DECLARE @tbl TABLE
(
phone_nr varchar(10),
call_time TIME
);
INSERT INTO @tbl VALUES ('726028xxx', '00:07:07');
INSERT INTO @tbl VALUES ('735560css', '00:07:37');
INSERT INTO @tbl VALUES ('726028xxx', '00:07:55');
SELECT phone_nr, SUM(DATEPART(minute, call_time)) AS total_call_time FROM @tbl GROUP BY phone_nr;
结果:
phone_nr | minutes
726028xxx | 14
735560css | 7
修改强>:
此版本与上述版本相同,不同之处在于它还考虑了秒数和小时数(例如1小时7分07秒= 67.117分钟),因此它更准确。
SELECT
phone_nr,
SUM(CAST(DATEPART(second, call_time) + (DATEPART(minute, call_time) * 60) + (DATEPART(hour, call_time) * 3600) AS decimal) / 60) AS total_call_time
FROM
@tbl
GROUP BY
phone_nr;
如果第一条记录为01:07:07
,则会产生以下结果。
phone_nr | minutes
726028xxx | 75.033332
735560css | 7.616666
答案 1 :(得分:1)
您可以使用DATEDIFF获取自“零”时间以来已经过的总分钟数
SELECT
[PHONE_NR],
SUM(DATEDIFF(minute, '00:00:00', [TIME]))
FROM
[YourTable]
GROUP BY
[PHONE_NR]
答案 2 :(得分:0)
declare @v1 varchar(50)
declare @v2 varchar(50)
set @v1='03:10'
set @v2='01:50'
--select cast(left(v1,2) as int) + cast(left(v2,2)as int)
select
cast(
cast(left(@v1,2) as int) + cast(left(@v2,2)as int)
+(cast(right(@v1,2) as int) + cast(right(@v2,2) as int))/60 as varchar)
+':'+cast(( cast(right(@v1,2) as int) + cast(right(@v2,2) as int))%60 as varchar)