Calculate number of concurrent events in SQL

后端 未结 4 1705
攒了一身酷
攒了一身酷 2020-12-15 07:52

I have a table that holds phone calls, with the following fields:

  • ID
  • STARTTIME
  • ENDTIME
  • STATUS
  • CALL_FROM
  • CALL_TO
4条回答
  •  臣服心动
    2020-12-15 08:35

    Try this in lieu of your between and a cross join:

    select
        t1.sid,
        count(1) as CountSimultaneous
    from
       calls_nov t1
       inner join nov t2 on
           t1.starttime <= t2.endtime
           and t1.endtime >= t2.starttime
    where
        t1.starttime between '2011-11-02' and '2011-11-03'
    group by
        t1.sid
    order by CountSimultaneous desc
    

提交回复
热议问题