Count the number of records valid between two dates

I am trying to execute a query to restore the number of active calls within a given time,

I have a CALLS table with all call records with AnsweredTime and ReleaseTime, and I need to get the number of calls active every second during the day.

So, I did 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 request returns this (in part):

 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 a good result, but the query is extremely slow, I did the indexing, I'm just wondering if there is anything else I can do.

+4
source share
1 answer

This is an interesting Cartesian that you will 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 line every second when the asset is active ... then this is a simple account. Hope my syntax is correct, I don't have ms sql server environment to confirm.

ETA: the left connection was used, so any seconds with 0 calls with a zero counter will be displayed here. If you switch to the inner join, the line will disappear within this interval of 1 second.

+5
source

Source: https://habr.com/ru/post/1382953/


All Articles