首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自联接的替代方案

自联接的替代方案
EN

Stack Overflow用户
提问于 2019-08-21 08:27:00
回答 3查看 165关注 0票数 0

我有一个表格-供应网络,包括四栏:

CustomerID,SupplierID,Supplier_productID,Purchase_Year

我想建立一个客户对,其中两个客户购买同一产品从同一供应商在一个重点年。我用self-joinBigQuery.But中做这件事--太慢了。有别的选择吗?

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

发布于 2019-08-21 08:30:31

使用联接语法并执行索引CustomerID列

代码语言:javascript
复制
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 
票数 0
EN

Stack Overflow用户

发布于 2019-08-21 12:02:46

您可以使用聚合来获取满足单行条件的所有客户:

代码语言:javascript
复制
select Purchase_Year, Supplier_productID, SupplierID,
       array_agg(distinct CustomerID) as customers
from supplynetwork sn
group by Purchase_Year, Supplier_productID, SupplierID;

然后可以使用数组操作获得对:

代码语言:javascript
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2019-08-21 15:24:22

您可以使用CROSS JOIN,这可能会给您带来简单的好处(即使使用笛卡儿)。试试下面的查询,看看它是否比基线更便宜:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57587421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档