0

我有一个 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 秒。我计算了调节器 / 60 的方式,但是我怎样才能在 SQL 中做到这一点才能得到准确的结果?!

转发,非常感谢!

4

2 回答 2

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
于 2013-03-26T08:50:38.793 回答
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 秒,就像它应该的那样:)

于 2013-03-26T08:36:58.703 回答