0

我需要确定每个月以 10 分钟的增量接听多少电话。我有以下 SQL 查询可以在半小时内工作,但我无法让它以 10 分钟为增量工作。我可能缺少一些简单的东西。任何帮助将不胜感激。谢谢

select 
    count(*) as Callcount,
    case 
       when datepart(mi, PBXqtime) < 30 
          then dateadd(hh, datediff(hh, 0, PBXqtime) + 0, 0)
          else dateadd(mi, 30, dateadd(hh, datediff(hh, 0, PBXqtime) + 0, 0)) 
    end as time
from 
    callcomplete 
where 
    CustId = '3100472' 
    and PBXQTime between '11/1/11 0:0:00' and '11/30/11 23:59:00'
group by 
    case 
       when datepart(mi, PBXqtime) < 30 
        then dateadd(hh, datediff(hh, 0, PBXqtime) + 0, 0) 
        else dateadd(mi, 30, dateadd(hh, datediff(hh, 0, PBXqtime) + 0, 0)) 
    end
4

2 回答 2

1

我认为这会起作用(但未经测试,因为我没有你的数据):

select 
    count(*) as Callcount,
    DATEADD(minute,(DATEDIFF(minute,'20100101',PBXQTime) / 10)*10,'20100101')
from 
    callcomplete 
where 
    CustId = '3100472' 
    and PBXQTime >= '20111101'
    and PBXQTime < '20111201'
group by 
    DATEADD(minute,(DATEDIFF(minute,'20100101',PBXQTime) / 10)*10,'20100101')

只需取自某个固定时间点以来的分钟数,除以 10(将​​向下舍入到最接近的整数),然后乘以 10 并加回到同一固定时间点。

于 2012-09-11T14:54:53.723 回答
1

没有测试,但是:

SELECT
    DATEPART(DAY, pbxqtime) AS [day], DATEPART(HOUR, pbxqtime) AS [hour], 
    (DATEPART(MINUTE, pbxqtime) / 10) AS [interval], COUNT(*) AS Callcount
FROM
    callcomplete
WHERE
    custid = '3100472'
    AND pbxqtime BETWEEN '11/1/11 0:0:00' AND '11/30/11 23:59:00'
GROUP BY
    DATEPART(DAY, pbxqtime), DATEPART(HOUR, pbxqtime), 
    (DATEPART(MINUTE, pbxqtime) / 10)

这是假设一个 MSSQL 环境。此外,关于如何指定日期范围,下面还有一场激烈的争论。有人争辩说,使用简单的运算符,例如>=and<=工作得更好,更干净。因此,您可以选择使用AND pbxqtime >= '2011-11-01' AND pbxqtime < '2011-12-01'.

于 2012-09-11T14:27:11.843 回答