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

后端 未结 6 1105
滥情空心
滥情空心 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:41

    I can see only one approach that extracts the data as specified from the call records:

    Create a list of events, where event is defined as the beginning of a call or the end of a call. (Thus each call record will generate two events.) Each event item should contain: system, datetime, and the boolean begin/end. Datetime should be rounded down to the nearest minute.

    Sort this list by (system, datetime) and scan it. For each call begin, increment CURCNT by one. For each call end, decrement CURCNT by one.

    If the datetime value is different from the previous record, add CURCNT to HOURSUM. If the datetime value indicates the start of a new hour, divide HOURSUM by 60, write a new result record (system, date, hour, average), and reset HOURSUM to zero.

    It should be obvious when to initialize CURCNT and HOURSUM, what to do when the system value is different from the previous record, etc.

    -Al.

提交回复
热议问题