[英]SQL Presto Query - Retrieving all possible combinations of rows?
我想知道如何才能將交叉連接表的所有可能組合賦給自己?
樣品表看起來像
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分鍾之內創建的,並且彼此之間的取貨地點相距3英里,而彼此之間的下車地點相距3英里。
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
之后條件為真的行。 您可能還希望施加最小距離,以排除一行與自身的匹配項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.