[英]Alternative to self-join
我有一個表格供應網絡,其中包括四列:
客戶ID,供應商ID,供應商_產品ID,采購年
。
我想構建一個客戶對,其中兩個客戶都需要在同一個年度內從同一供應商處購買相同的產品。 我在BigQuery
使用self-join
來完成此操作,但這太慢了。 還有其他選擇嗎?
select distinct
a.CustomerID as focal_CustomerID,
b.CustomerID as linked_CustomerID,
a.Purchase_Year,
a.Supplier_productID
from
supplynetwork as a,
supplynetwork as b
where
a.CustomerID<>b.CustomerID and
a.Purchase_Year=b.Purchase_Year and
a.Supplier_productID=b.Supplier_productID and
a.SupplierID=b.SupplierID
使用聯接語法並為CustomerID列編制索引
select distinct
a.CustomerID as focal_CustomerID,
b.CustomerID as linked_CustomerID,
a.Purchase_Year,
a.Supplier_productID
from
supplynetwork as a join
supplynetwork as b
on
a.Purchase_Year=b.Purchase_Year and
a.Supplier_productID=b.Supplier_productID and
a.SupplierID=b.SupplierID
where a.CustomerID<>b.CustomerID
您可以使用聚合在一行中獲取所有滿足條件的客戶:
select Purchase_Year, Supplier_productID, SupplierID,
array_agg(distinct CustomerID) as customers
from supplynetwork sn
group by Purchase_Year, Supplier_productID, SupplierID;
然后,您可以使用數組操作獲取對:
with pss as (
select Purchase_Year, Supplier_productID, SupplierID,
array_agg(distinct CustomerID) as customers
from supplynetwork sn
group by Purchase_Year, Supplier_productID, SupplierID
)
select c1, c2, pss.*
from pss cross join
unnest(pss.customers) c1 cross join
unnest(pss.customers) c2
where c1 < c2;
您可以使用CROSS JOIN
,它(即使是笛卡爾的)也可以使您受益於簡單性。 請在下面嘗試以下查詢,看看它是否比您的基准便宜:
select
focal_CustomerID,
linked_CustomerID,
Purchase_Year,
Supplier_ProductID
from (
select
SupplierID,
Supplier_ProductID,
Purchase_Year,
array_agg(distinct CustomerID) as Customers
from `mydataset.mytable`
group by 1,2,3
), unnest(Customers) focal_CustomerID
cross join unnest(Customers) linked_CustomerID
where focal_CustomerID != linked_CustomerID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.