1

我在 MS SQL 2012(40m 记录)中有一个包含呼叫数据的大表。我想找到呼叫的高峰量,以及它发生的时间。如果可能的话,我还想找出接下来的 4 个最繁忙的时期。

我计划使用 3 列: CallID DialTime EndTime

我能想到的唯一方法是这样做:

Select '2013-07-01 00:00:01' as [Period], count([CallID]) as [Calls]
from [Table]
where DialTime <= '2013-07-01 00:00:01'
and EndTime >= '2013-07-01 00:00:01'

union

Select '2013-07-01 00:00:02' as [Period], count([CallID]) as [Calls]
from [Table]
where DialTime <= '2013-07-01 00:00:02'
and EndTime >= '2013-07-01 00:00:02'

union

etc

任何人都可以提出更好/更有效的方法吗?

4

2 回答 2

0

尝试这样的事情。@time_begin并且@time_end是您可以在想要获得结果的时间间隔中使用的参数。

with time_items (time_item) as
(
    select @time_begin as time_item
    union all
    select dateadd(second,1,t.time_item) as time_item from time_items t where t.time_item<@time_end
)
select
    time_items.time_item as [Period],
    sum(case when [Table].DialTime<=time_items.time_item and [Table].EndTime>=time_items.time_item then 1 else 0 end) as [Calls]
from time_items
left outer join [Table] on 1=1
group by
    time_items.time_item
order by
    [Calls] desc;
于 2013-07-09T08:20:57.367 回答
0

您可以使用 VALUES 作为表源

SELECT DialTime, EndTime, o.Calls
FROM (VALUES ('20130701 00:00:01', '20130701 00:00:01'),
             ('20130701 00:00:02', '20130701 00:00:02'))x(DialTime, EndTime)
  CROSS APPLY(
              SELECT COUNT(CallID) AS Calls      
              FROM [Table] t
              WHERE DialTime <= x.DialTime
                AND EndTime >= x.EndTime
              ) o
于 2013-07-09T09:51:35.937 回答