如何查找每行两个日期之间的行
How to find rows between two dates per row
我有以下 table:
topic_id conversation logical_date start_date end_date type
1 1 2020-01-01 09:00 2020-01-01 09:00 2020-01-01 09:50 phone call
1 2 2020-01-01 09:14 text
1 3 2020-01-01 10:27 text
2 1 2020-02-03 08:40 text
此 table 表示支持请求。每个支持请求都有主题,主题有 1 个或多个对话。
我想查找在 phone 调用的 start_date 和 end_date 之间完成的所有文本请求。
所以对于上面的table我想看:
topic_id conversation_id start_date end_date sum
1 1 2020-01-01 09:00 2020-01-01 09:50 1
逻辑是:
对于每个 topic_id,type='phone call' 取 start_date 和 end_date
将它们与此 topic_id 中的 type='text' 对话进行比较
对 logical_date 在 start_date 和 end_date
之间的人求和
我知道我需要用 window 函数来做这个,但我不确定怎么做。
这是我目前拥有的:
select topic_id, conversation_id, start_date, end_date, count(1 ) over partition by ()
from table
where type = 'phone call'
我正在使用 Presto
我想你想要:
select t.*
from t
where t.type = 'text' and
exists (select 1
from t t2
where t2.conversation_id = t.conversation_id and
t.logical_date between t2.start_date and t2.end_date and
t2.type = 'phone'
);
如果您确实需要两个记录的信息,请使用 join
:
select tt.*, tp.*
from t tt join
t tp
on tp.conversation_id = tt.conversation_id and
tt.logical_date between tp.start_date and tp.end_date and
tp.type = 'text' and
tp.type = 'phone';
您可以使用相关子查询来计算可以为每个 'phone call'
条记录找到相同 topic_id
的 'text'
条记录:
select
t.*,
(
select count(*)
from mytable t1
where
t1.topic_id = t.topic_id
and t1.type = 'text'
and t1.logical_date >= t.start_date
and t1.logical_date < t.end_date
) cnt
from mytable t
where t.type = 'phone call'
为了性能,您需要 (topic_id, type, logical_date)
上的索引。
我不确定使用 window 函数是否更简单。
我有以下 table:
topic_id conversation logical_date start_date end_date type
1 1 2020-01-01 09:00 2020-01-01 09:00 2020-01-01 09:50 phone call
1 2 2020-01-01 09:14 text
1 3 2020-01-01 10:27 text
2 1 2020-02-03 08:40 text
此 table 表示支持请求。每个支持请求都有主题,主题有 1 个或多个对话。
我想查找在 phone 调用的 start_date 和 end_date 之间完成的所有文本请求。
所以对于上面的table我想看:
topic_id conversation_id start_date end_date sum
1 1 2020-01-01 09:00 2020-01-01 09:50 1
逻辑是: 对于每个 topic_id,type='phone call' 取 start_date 和 end_date 将它们与此 topic_id 中的 type='text' 对话进行比较 对 logical_date 在 start_date 和 end_date
之间的人求和我知道我需要用 window 函数来做这个,但我不确定怎么做。
这是我目前拥有的:
select topic_id, conversation_id, start_date, end_date, count(1 ) over partition by ()
from table
where type = 'phone call'
我正在使用 Presto
我想你想要:
select t.*
from t
where t.type = 'text' and
exists (select 1
from t t2
where t2.conversation_id = t.conversation_id and
t.logical_date between t2.start_date and t2.end_date and
t2.type = 'phone'
);
如果您确实需要两个记录的信息,请使用 join
:
select tt.*, tp.*
from t tt join
t tp
on tp.conversation_id = tt.conversation_id and
tt.logical_date between tp.start_date and tp.end_date and
tp.type = 'text' and
tp.type = 'phone';
您可以使用相关子查询来计算可以为每个 'phone call'
条记录找到相同 topic_id
的 'text'
条记录:
select
t.*,
(
select count(*)
from mytable t1
where
t1.topic_id = t.topic_id
and t1.type = 'text'
and t1.logical_date >= t.start_date
and t1.logical_date < t.end_date
) cnt
from mytable t
where t.type = 'phone call'
为了性能,您需要 (topic_id, type, logical_date)
上的索引。
我不确定使用 window 函数是否更简单。