I am anaylzing some Call Detail Records to determine Link Capacities for a Voice Switch. Call Detail Records hold, amongst other things, Originating Number, Destination Number, Time and Duration of the Call.The key thing I want to determine is the peak number of concurrent calls per geographic region and carrier. I can easily determine the region and carrier from the destination number, so I am trying to determine the peak callsI am currently doing thisCREATE TABLE #PeakCallAnalysis ( [CDR_ID] [bigint], [StartDate] [DATETIME], [EndDate] [DATETIME]) -- Some Rows of Test DataINSERT INTO #PeakCallAnalysis SELECT 13080, '2009-01-01 00:01:00.000', '2009-01-01 00:03:00.000'UNIONSELECT 24802, '2009-01-01 00:01:00.000', '2009-01-01 00:04:00.000'UNIONSELECT 24975, '2009-01-01 00:02:00.000', '2009-01-01 00:07:00.000'UNIONSELECT 14684, '2009-01-01 00:02:00.000', '2009-01-01 00:04:00.000'UNIONSELECT 17195, '2009-01-01 00:03:00.000', '2009-01-01 00:05:00.000'UNIONSELECT 38851, '2009-01-01 00:03:00.000', '2009-01-01 00:19:00.000'UNIONSELECT 33572, '2009-01-01 00:04:00.000', '2009-01-01 00:11:00.000'UNIONSELECT 31336, '2009-01-01 00:04:00.000', '2009-01-01 00:10:00.000'UNIONSELECT 21806, '2009-01-01 00:05:00.000', '2009-01-01 00:07:00.000'UNIONSELECT 21688, '2009-01-01 00:05:00.000', '2009-01-01 00:06:00.000'UNIONSELECT 23641, '2009-01-01 00:07:00.000', '2009-01-01 00:08:00.000'UNIONSELECT 27440, '2009-01-01 00:07:00.000', '2009-01-01 00:10:00.000'UNIONSELECT 33053, '2009-01-01 00:08:00.000', '2009-01-01 00:11:00.000'UNIONSELECT 37278, '2009-01-01 00:08:00.000', '2009-01-01 00:13:00.000'UNIONSELECT 36546, '2009-01-01 00:08:00.000', '2009-01-01 00:12:00.000'UNIONSELECT 30996, '2009-01-01 00:08:00.000', '2009-01-01 00:10:00.000'UNIONSELECT 42371, '2009-01-01 00:09:00.000', '2009-01-01 00:21:00.000'UNIONSELECT 34398, '2009-01-01 00:09:00.000', '2009-01-01 00:11:00.000'UNIONSELECT 41011, '2009-01-01 00:10:00.000', '2009-01-01 00:15:00.000'UNIONSELECT 31086, '2009-01-01 00:11:00.000', '2009-01-01 00:13:00.000'SELECT CDR_ID, StartDate, EndDate, /* Find all the calls that started at the same time or before this one and haven't finished yet*/ (SELECT COUNT(*) FROM #PeakCallAnalysis A2 WHERE A1.startdate >= A2.StartDate AND A1.startdate < A2.Enddate) ConcurrentCallsFROM #PeakCallAnalysis A1ORDER BY StartDateDROP TABLE #PeakCallAnalysis
This works well enough and gives me the right answer. However to do a proper analysis I need to analyse tens of million of records and my correlated subquery that doesn't even have a proper join seems like a bad idea.Is there a better way to do it?