大家好,我正在尝试解决以下问题:假设我有一张桌子
事件类型 | 聊天 | 时间_开始 | 时间结束 |
---|---|---|---|
A | 聊天1 | 15:01:21 | 15:04:20 |
A | 聊天2 | 15:00:13 | 15:03:10 |
A | 聊天3 | 15:03:00 | 15:17:00 |
A | 聊天4 | 14:50:00 | 15:03:05 |
B | 聊天1 | 17:00:00 | 17:30:00 |
我的目标是输出每个 event_type 和每个聊天与其他聊天的交集数量。 也就是说,我希望结果看起来像这样
事件类型 | 聊天 | 交叉路口数量 |
---|---|---|
A | 聊天1 | 4 |
A | 聊天2 | 4 |
A | 聊天3 | 4 |
A | 聊天4 | 4 |
B | 聊天1 | 0 |
我知道我可以使用JON,但是我的真实数据库非常大,并且它不加载连接。 所以我想到了以下查询
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.
但是结果不是我需要的。有谁知道如何解决这个问题吗?
假设您确实想要:-
即按照
我的目标是为每个 event_type 和每个聊天输出与 other 聊天的交集数。
那么你可以使用:-
SELECT
event_type,
chat,
(
SELECT count()
FROM events AS t2
WHERE
t2.rowid <> t1.rowid
AND
(
t1.time_start BETWEEN t2.time_start AND t2.time_end
OR
t1.time_end BETWEEN t2.time_start AND t2.time_end
)
) AS NOI
FROM events AS t1
ORDER BY event_type,chat
;
WITHOUT ROWID
表,尽管您可以使用 t2.event_type||t2.chat <> t1.event_type||t1.chat
而不是 t2.rowid <> t1.rowid
来实现相同的结果。