我想知道如何获得交叉连接表的所有可能组合?
样本表看起来像
DAY Order pickup_lat pickup_long dropoff_lat dropoff_long created_time
1/3/19 234e 32.69 -117.1 32.63 -117.08 3/1/19 19:00
1/3/19 235d 40.73 -73.98 40.73 -73.99 3/1/19 23:21
1/3/19 253w 40.76 -73.99 40.76 -73.99 3/1/19 15:26
2/3/19 231y 36.08 -94.2 36.07 -94.21 3/2/19 0:14
3/3/19 305g 36.01 -78.92 36.01 -78.95 3/2/19 0:09
3/3/19 328s 36.76 -119.83 36.74 -119.79 3/2/19 4:33
3/3/19 286n 35.76 -78.78 35.78 -78.74 3/2/19 0:43
我想根据订单的差异来查看所有可能的组合,这些订单的差异在创建时间和距离上以数英里的距离进行拾取和下车。这可能吗?
我将使用 great_circle_distance(pickup_lat,pickup_lng, pickup_1_lat, pickup_1_lng)*0.621371)
进行距离计算的距离,互相取货和下车。
和date_diff('minute', created_time, created_time_1) as order_creation_delta
因此,在彼此创建的3分钟内,距一个Anothers拾音位置和距一个Anothers下车位置3英里的任何2个订单或一对订单。
with data as
( select
a.business_day,
a.delivery_uuid,
a.order_created_time_utc,
a.pickup_lat,
a.pickup_lng,
a.dropoff_lat,
a.dropoff_lng
from integrated_delivery.managed_delivery_fact a
where a.business_day between (timestamp '2019-03-01') and (timestamp '2019-03-03')
union
select b.business_day as b_business_day,
b.delivery_uuid as b_delivery_uuid,
b.order_created_time_utc as b_order_created_time_utc,
b.pickup_lat as b_pickup_lat,
b.pickup_lng as b_pickup_lng,
b.dropoff_lat as b_dropoff_lat,
b.dropoff_lng as b_dropoff_lng
from integrated_delivery.managed_delivery_fact b
where b.business_day between (timestamp '2019-03-01') and (timestamp '2019-03-03')
)
stats as
( select abs(date_diff('minute', a.order_created_time_utc, b.order_created_time_utc)) as order_creation_difference,
(great_circle_distance(a.pickup_lat, a.pickup_lng, b.pickup_lat, b.pickup_lng)*0.621371) as pickup_distance,
(great_circle_distance(a.dropoff_lat, a.dropoff_lng, b.dropoff_lat, b.dropoff_lng)*0.621371) as dropoff_distance
from data
)
select a.delivery_uuid, b.delivery_uuid, order_creation_difference, pickup_distance, dropoff_distance
from data a
cross join data b
WHERE a.delivery_uuid <> b.delivery_uuid
and order_creation_difference <3
and pickup_distance < 3
and dropoff_distance <3
我有一个类似上述查询的查询,但不确定如果我在表格之前与表结合,我是否可以将值计算为CTE?
看来您需要加入而不是联合。
with a as (select * from your_table)
select * from your_table
inner join a on
great_circle(a.lat, a.long, your_table.lat, your_table.long) < max_dist
and abs(date_diff('min', a. date, your_table. date)) < max_time
说明:两个表中的inner join
输出全部,唯一的组组合,而on
后的条件为True。您可能还需要施加最小距离,以排除与自身的匹配。