所以,假设我有表格 - 动作和记录看起来像这样:
动作表:
action_id customer start_time end_time 101 A 2016-01-01 15:00 2016-01-02 15:00 102 B 2016-01-01 15:00 2016-01-01 20:00 103 A 2016-01-01 23:00 2016-01-02 23:00
记录表:
rec_id customer time 1001 A 2016-01-01 16:00 1002 A 2016-01-01 20:00 1003 A 2016-01-02 17:00 1004 B 2016-01-01 15:50
我想计算在第一个表的start_time和end_time内发生的第二个表中的所有记录,并通过action_id,customer对其进行分组。
我的预期输出应为:
action_id customer count (rec_id) 101 A 2 102 B 1 103 A 1
感谢您的帮助
答案 0 :(得分:0)
select
a.action_id,a.customer,count(*) as cnt
from
records r
join
Actions a
on r.customer=a.customer and
r.time between a.start_time and a.end_time
group by a.action_id,a.customer