Mysql workaround for window functions

前端 未结 4 993
南方客
南方客 2020-12-19 06:52

I have an event table that has the following fields:

event_id
event_type 
event_time

Given a duration D and a number k<

4条回答
  •  时光取名叫无心
    2020-12-19 06:58

    Edit: Rearranged whole answer

    Now I understand what you expect.

    I've created such a test table on my MySQL and this seems to work:

    SELECT e2.event_type FROM events e1
    JOIN events e2 
        ON e1.event_time BETWEEN e2.event_time AND (e2.event_time + INTERVAL 10 MINUTE);
    GROUP BY e1.event_id, e2.event_type
    HAVING count(e2.event_type) >= 5
    

    Basically, for each event you self join events with specified relative time window (from event_time to event_time + window duration), and then you group by e1's even_id to get emulated floating time window. Also we're gruping by event_type here because you want to get this field values for each window.

    All you need to think through is performance. I'm not sure if it will be efficient enough for a 1M of records.

提交回复
热议问题