Count the number of records active between two dates

danjam picture danjam · Nov 24, 2011 · Viewed 9.7k times · Source

I am trying to do a query to retreive the number of active calls for a given time,

I have a table CALLS with all the calls record with an AnsweredTime and ReleaseTime and I need to retrieve the number of calls active for every second during the day.

So I've done the following :

    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 is a table containing every second between two dates

The query return this (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

So I got the good result but the query is extremly slow, I've done the indexing, I am just wondering if there is something more I can do..

Answer

Twelfth picture Twelfth · Nov 24, 2011

It's an interesting cartesian that you'll create here.

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

I believe this should give you one row for every second a call is active...then it's a simple count. Hoping my syntax is correct, I don't have a ms sql server environment to confirm on.

ETA: Left join was used so any seconds with 0 calls will appear on here with a null count. If you switch to an inner join, the row will disappear for that 1 second interval.