SQL将秒转换为分钟到小时

时间:2013-03-25 13:34:19

标签: sql date sql-server-2012

我有一个SQL查询,用于计算IP语音对话的总量。

SELECT COUNT(*) AS 'Aantal gesprekken'
,FromUri AS 'Medewerker'
,SUM(DATEDIFF(hour,InviteTime,EndTime)) AS 'Uren'
,SUM(DATEDIFF(minute,InviteTime,EndTime)) AS 'Minuten'
,SUM(DATEDIFF(second,InviteTime,EndTime)) AS 'Secondes'
   FROM [LcsCDR].[dbo].[SessionDetailsView]
WHERE FromUri LIKE '%robert%'
AND (CAST([InviteTime] AS date)) BETWEEN '2012-12-27' AND '2013-01-28'
AND MediaTypes = '16'
GROUP BY FromUri

SQL Server 2012提供此结果:数字,员工,小时,分钟和秒。

302 robert  11  518 31316

但我需要得到这个结果:28小时19分56秒。我计算了reguler / 60方式,但我怎样才能在SQL中做到这一点,我得到了准确的结果?!

前进,非常感谢!

2 个答案:

答案 0 :(得分:1)

尝试以下查询

SELECT COUNT(*) AS 'Aantal gesprekken',FromUri AS 'Medewerker',                
concat(
  floor(SUM(DATEDIFF(second,InviteTime,EndTime))/3600),":",   //Hrs
  floor(SUM(DATEDIFF(second,InviteTime,EndTime))/60)%60,":",  //Mins
  SUM(DATEDIFF(second,InviteTime,EndTime))%60                 //Secs
  ) as Total_Hrs
   FROM [LcsCDR].[dbo].[SessionDetailsView]
WHERE FromUri LIKE '%robert%'
AND (CAST([InviteTime] AS date)) BETWEEN '2012-12-27' AND '2013-01-28'
AND MediaTypes = '16'
GROUP BY FromUri

答案 1 :(得分:0)

在Steoleary的帮助下,我已经设法解决了这个问题

DECLARE @SecondsToConvert int
SET @SecondsToConvert = (SELECT (SUM(DATEDIFF(hour,InviteTime,EndTime) * 3600) + SUM(DATEDIFF(minute,InviteTime,EndTime) * 60) + SUM(DATEDIFF(second,InviteTime,EndTime) * 1)) AS [Seconds] 
 FROM [LcsCDR].[dbo].[SessionDetailsView]
WHERE FromUri LIKE '%robert%'
AND (CAST([InviteTime] AS date)) BETWEEN '2012-12-27' AND '2013-01-28'
AND MediaTypes = '16'
GROUP BY FromUri)

-- Declare variables
 DECLARE @Hours int
 DECLARE @Minutes int
 DECLARE @Seconds int

-- Set the calculations for hour, minute and second
SET @Hours = @SecondsToConvert/3600
SET @Minutes = (@SecondsToConvert % 3600) / 60
SET @Seconds = @SecondsToConvert % 60

SELECT COUNT(*) AS 'Aantal gesprekken'
,FromUri AS 'Medewerker'
,@Hours AS 'Uren' ,@Minutes AS 'Minuten' , @Seconds AS 'Seconden'
 FROM [LcsCDR].[dbo].[SessionDetailsView]
WHERE FromUri LIKE '%robert%'
AND (CAST([InviteTime] AS date)) BETWEEN '2012-12-27' AND '2013-01-28'
AND MediaTypes = '16'
GROUP BY FromUri

结果,我现在得到了准确的时间。

302 robert  28  19  56

28小时19分56秒,就像它应该是:)