【发布时间】:2019-08-21 08:27:00
【问题描述】:
我有一个 table-supplynetwork 包括四列:
CustomerID、SupplierID、Supplier_productID、Purchase_Year
.
我想构建一个客户对,其中两个客户在一个焦点年度从同一供应商处购买同一产品。
我使用self-join 在BigQuery 中执行此操作。但它太慢了。有什么选择吗?
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
【问题讨论】:
-
今日提示:始终使用现代、明确的
JOIN语法。更容易编写(没有错误),更容易阅读(和维护),并且在需要时更容易转换为外连接。 (请注意,这不是您的性能问题的答案。) -
您应该切换到
a.CustomerID < b.CustomerID以避免重复。现在你得到 A、B 和 B、A。
标签: sql google-bigquery