Hello everyone I'm trying to solve the following problem: let's say I have a table with
| event_type | chat | time_start | time_end |
|---|---|---|---|
| A | chat 1 | 15:01:21 | 15:04:20 |
| A | chat 2 | 15:00:13 | 15:03:10 |
| A | chat 3 | 15:03:00 | 15:17:00 |
| A | chat 4 | 14:50:00 | 15:03:05 |
| B | chat 1 | 17:00:00 | 17:30:00 |
My goal is to output for each event_type and each chat the number of intersections with other chats. That is, I want the result to look like this
| event_type | chat | Number of intersections |
|---|---|---|
| A | chat 1 | 4 |
| A | chat 2 | 4 |
| A | chat 3 | 4 |
| A | chat 4 | 4 |
| B | chat 1 | 0 |
I understand that I can use JON, but my real database is very large, and it does not load join's. So I thought of the following query
WITH T1
WITH Intervals AS (
SELECT
time_start AS Event_time,
1 AS cnt,
event_type
FROM
events
UNION ALL
SELECT
time_end AS Event_time,
-1 AS cnt,
event_type
FROM
events
)
SELECT
event_type, chat,
SUM(cnt) OVER (PARTITION BY event_type ORDER BY Event_time) AS Cumulative_count
FROM
Intervals
GROUP BY event_type,chat.
But the result is not what I need. Does anyone have any idea how to solve this?
Assuming that you actually want:-
i.e. as per
then you could use:-
WITHOUT ROWIDtable, although you could uset2.event_type||t2.chat <> t1.event_type||t1.chat, instead oft2.rowid <> t1.rowidto achieve the same result.count()withcount() + (count() > 0)and thus get:-