How can I check for average concurrent events in a SQL table based on the date, time and duration of the events?

后端 未结 6 1045
滥情空心
滥情空心 2021-02-05 22:48

I have a set of call detail records, and from those records, I\'m supposed to determine the average concurrent active calls per system, per hour (at a precision of one minute).

6条回答
  •  花落未央
    2021-02-05 23:42

    If I understand you correctly, you want to get a count of all records for which the start time is less then t+60 seconds and the start time plus the duration is less than or equal to t, for each t in the interval of interest (e.g., t=7:00, 7:01, 7:02...etc.).

    Then it's just a matter of averaging these counts.

    But what is an average? It's just the sum divided by the number of items, right? In this case, the number of items will always be equal to the time range in minutes, and the sum will be equal to the sum of the durations-minutes that fall within the interval, which you can compute in one go off the data given.

    Sound less impossible now? In pseudo SQL:

    select sum( 
         ((time+duration rounded up to next minute, capped at end of period)
        - (time rounded down, bottom-capped at start of period) - 1)
         /(1 minute) )
      from Records
      where date is right
    

    Then just divide that by the number of minutes in the period of interest.

提交回复
热议问题