0

我有两个表,“呼叫”和“订单”,并试图从“呼叫”表和“订单”表中获取“首次呼叫”时间和“首次订购”时间之间的时差,请参阅附件示例。

在此处输入图像描述

4

1 回答 1

1

请尝试以下 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);
于 2021-09-07T06:27:24.770 回答