调用 table 和订单 table 的首次调用时间和首次订购时间之间的时间差

Time difference between First Call time and First Order Time from Call table and Order table

我有两个 tables,'Call' 和 'Order,并试图从通话中获取 'First Call' 时间和 'First Order' 时间之间的时差 table和订单table,请看附件

请尝试以下SQL看看是否有帮助:

create or replace table call_table (key varchar, call_time timestamp);
create or replace table order_table (key varchar, order_time timestamp);

insert into call_table values 
('key_1', '2021-09-04 10:10:00'),
('key_1', '2021-09-04 10:14:00'),
('key_2', '2021-09-04 10:17:00'),
('key_2', '2021-09-04 10:18:00'),
('key_2', '2021-09-04 10:19:00'),
('key_2', '2021-10-01 11:20:00');

insert into order_table values 
('key_1', '2021-09-04 10:15:00'),
('key_1', '2021-09-04 10:18:00'),
('key_1', '2021-09-04 10:19:00'),
('key_2', '2021-09-04 10:22:00'),
('key_2', '2021-09-04 10:23:00'),
('key_2', '2021-09-04 10:24:00');


with my_call as (
  select key, min(call_time) as first_time
  from call_table
  group by key
),
my_order as (
  select key, min(order_time) as first_time
  from order_table
  group by key
)
select 
  c.key, 
  c.first_time, 
  o.first_time, 
  datediff('min', c.first_time, o.first_time) as time_diff
from my_call c
join my_order o ON (c.key = o.key);