我正在尝试进行查询,以检索给定时间内的活动呼叫数量,
我有一个表CALLS,其中所有的呼叫记录都带有AnsweredTime和ReleaseTime,我需要检索白天每秒激活的呼叫数。
所以我做了以下事情:
SELECT dt,
(SELECT COUNT(Id) FROM Calls WHERE
AnsweredTimestamp <= dt AND
ReleasedTimestamp >= dt)
FROM CALView
WHERE dt >='2011-10-05'
and dt <'2011-10-06'
CalView是一个包含两个日期之间每秒的表格
查询返回此(partialy):
2011-10-05 09:40:00.000 20
2011-10-05 09:40:01.000 20
2011-10-05 09:40:02.000 20
2011-10-05 09:40:03.000 21
2011-10-05 09:40:04.000 21
2011-10-05 09:40:05.000 21
2011-10-05 09:40:06.000 21
2011-10-05 09:40:07.000 21
2011-10-05 09:40:08.000 21
2011-10-05 09:40:09.000 21
2011-10-05 09:40:10.000 20
2011-10-05 09:40:11.000 20
2011-10-05 09:40:12.000 19
2011-10-05 09:40:13.000 19
2011-10-05 09:40:14.000 19
2011-10-05 09:40:15.000 19
2011-10-05 09:40:16.000 19
2011-10-05 09:40:17.000 19
2011-10-05 09:40:18.000 19
2011-10-05 09:40:19.000 19
2011-10-05 09:40:20.000 18
2011-10-05 09:40:21.000 18
2011-10-05 09:40:22.000 18
2011-10-05 09:40:23.000 18
2011-10-05 09:40:24.000 18
2011-10-05 09:40:25.000 18
2011-10-05 09:40:26.000 18
2011-10-05 09:40:27.000 18
2011-10-05 09:40:28.000 18
2011-10-05 09:40:29.000 18
2011-10-05 09:40:30.000 18
2011-10-05 09:40:31.000 18
2011-10-05 09:40:32.000 18
2011-10-05 09:40:33.000 18
2011-10-05 09:40:34.000 18
2011-10-05 09:40:35.000 19
2011-10-05 09:40:36.000 18
2011-10-05 09:40:37.000 18
2011-10-05 09:40:38.000 18
2011-10-05 09:40:39.000 18
2011-10-05 09:40:40.000 18
2011-10-05 09:40:41.000 18
2011-10-05 09:40:42.000 18
2011-10-05 09:40:43.000 18
2011-10-05 09:40:44.000 19
2011-10-05 09:40:45.000 19
2011-10-05 09:40:46.000 19
2011-10-05 09:40:47.000 19
2011-10-05 09:40:48.000 19
2011-10-05 09:40:49.000 19
2011-10-05 09:40:50.000 19
2011-10-05 09:40:51.000 19
2011-10-05 09:40:52.000 19
2011-10-05 09:40:53.000 19
2011-10-05 09:40:54.000 19
2011-10-05 09:40:55.000 19
2011-10-05 09:40:56.000 19
2011-10-05 09:40:57.000 19
2011-10-05 09:40:58.000 20
2011-10-05 09:40:59.000 20
2011-10-05 09:41:00.000 20
2011-10-05 09:41:01.000 20
2011-10-05 09:41:02.000 20
2011-10-05 09:41:03.000 20
2011-10-05 09:41:04.000 20
2011-10-05 09:41:05.000 20
2011-10-05 09:41:06.000 20
2011-10-05 09:41:07.000 20
2011-10-05 09:41:08.000 20
2011-10-05 09:41:09.000 19
2011-10-05 09:41:10.000 19
2011-10-05 09:41:11.000 19
2011-10-05 09:41:12.000 19
2011-10-05 09:41:13.000 19
2011-10-05 09:41:14.000 19
2011-10-05 09:41:15.000 19
2011-10-05 09:41:16.000 20
2011-10-05 09:41:17.000 20
2011-10-05 09:41:18.000 20
2011-10-05 09:41:19.000 20
2011-10-05 09:41:20.000 20
2011-10-05 09:41:21.000 20
2011-10-05 09:41:22.000 20
2011-10-05 09:41:23.000 20
2011-10-05 09:41:24.000 20
2011-10-05 09:41:25.000 20
2011-10-05 09:41:26.000 20
2011-10-05 09:41:27.000 20
2011-10-05 09:41:28.000 20
2011-10-05 09:41:29.000 20
2011-10-05 09:41:30.000 19
2011-10-05 09:41:31.000 19
2011-10-05 09:41:32.000 19
2011-10-05 09:41:33.000 19
2011-10-05 09:41:34.000 20
2011-10-05 09:41:35.000 20
2011-10-05 09:41:36.000 20
2011-10-05 09:41:37.000 19
2011-10-05 09:41:38.000 19
2011-10-05 09:41:39.000 19
2011-10-05 09:41:40.000 19
2011-10-05 09:41:41.000 19
2011-10-05 09:41:42.000 19
2011-10-05 09:41:43.000 19
2011-10-05 09:41:44.000 19
2011-10-05 09:41:45.000 19
2011-10-05 09:41:46.000 18
2011-10-05 09:41:47.000 18
2011-10-05 09:41:48.000 19
2011-10-05 09:41:49.000 20
2011-10-05 09:41:50.000 20
2011-10-05 09:41:51.000 20
2011-10-05 09:41:52.000 20
2011-10-05 09:41:53.000 20
所以我得到了很好的结果,但是查询非常慢,我已经完成了索引,我只是想知道是否还有更多我能做的事情。
答案 0 :(得分:6)
这是一个有趣的笛卡儿,你将在这里创建。
Select dt,count(1)
from calview
left join calls on calls.AnsweredTimestamp <= calview.dt AND
calls.ReleasedTimestamp >= calview.dt
WHERE dt >='2011-10-05'
and dt <'2011-10-06'
group by dt
我相信这应该给你一个呼叫有效的每一秒......然后这是一个简单的计数。希望我的语法正确,我没有ms sql server环境来确认。
ETA:使用了左连接,因此任何带有0次调用的秒数都将显示在此处,并显示空计数。如果切换到内部联接,该行将在1秒间隔内消失。