简体   繁体   English

如何使用 BigQuery 从另一个表中获取包含相同项目的交易?

[英]How do I get transactions that contains the same items from another table using BigQuery?

Suppose I have a BigQuery table containing a list of items, bundled together like this:假设我有一个包含项目列表的 BigQuery 表,像这样捆绑在一起:

Bundle Name捆绑名称 Product Name产品名称
Bundle 1捆绑包 1 Apple苹果
Bundle 1捆绑包 1 Watermelon西瓜
Bundle 2捆绑包 2 Grapes葡萄
Bundle 2捆绑包 2 Lemon柠檬

Then, I also have a BigQuery table containing a list of transactions, where a transaction can contain more than one product, like this:然后,我还有一个包含交易列表的 BigQuery 表,其中一个交易可以包含多个产品,如下所示:

Transactions ID交易编号 Product Name产品名称
Transactions 1交易 1 Apple苹果
Transactions 1交易 1 Watermelon西瓜
Transactions 2交易 2 Grapes葡萄
Transactions 2交易 2 Lemon柠檬
Transactions 2交易 2 Banana香蕉
Transactions 3交易 3 Pineapple菠萝
Transactions 3交易 3 Kiwi猕猴桃
Transactions 3交易 3 Grapes葡萄

I would like to get transactions that contain the exact item from the list of bundles.我想从捆绑列表中获取包含确切项目的交易。 In this case, I should get Transaction 1 since it has a product combination in a transaction that exactly matches Bundle 1 (Apple and Watermelon) and Transactions 2 because it also has a product combination that matches Bundle 2 (Grapes and Lemon).在这种情况下,我应该得到交易 1,因为它在交易中有一个与捆绑包 1(苹果和西瓜)完全匹配的产品组合,而交易 2 因为它也有一个与捆绑包 2(葡萄和柠檬)匹配的产品组合。

I can't use JOIN because if I join the Bundle table and Transaction table using Product Name, Transaction 3 will also be included since it has the same product name as Bundle 2 (Grapes).我不能使用 JOIN,因为如果我使用 Product Name 加入 Bundle 表和 Transaction 表,Transaction 3 也将被包括在内,因为它与 Bundle 2 (Grapes) 具有相同的产品名称。

How can I do this?我怎样才能做到这一点? Thanks in advance提前致谢

Consider below approach考虑以下方法

select transactionID, bundleName
from (
  select transactionID, array_agg(distinct productName) as products
  from transactions group by transactionID
) t1, (
  select bundleName, array_agg(distinct productName) as products
  from bundles group by bundleName
) t2
where (
  select count(product1) = count(product2)
  from t2.products product2
  left join t1.products product1
  on product1 = product2 
)           

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 python 中的 BigQuery 获取表名 - How can I get Table Name from BigQuery in python 如何将一个表中的列与 BigQuery 中另一个表中的数组进行比较? - How to compare column in one table with array from another table in BigQuery? 如何在 BigQuery Explorer 列表中显示来自另一个项目的数据集? - How do I display datasets from another project in BigQuery Explorer list? 如何使用 bigquery 计算每列的项目频率? - How do I count the frequency of items for each column with bigquery? 如何将多个文件(相同模式)从 LOCAL 加载到 BigQuery 中的表中? - How to load multiple files (same schema) from LOCAL into a table in BigQuery? 在 BigQuery 中使用 HyperLogLog 函数是否可以从对相同数据的相同查询中得到不同的结果? - Using HyperLogLog functions in BigQuery can you get different results from the same query on the same data? 如何将 BigQuery 中的细分导入 Firebase? - How do I import segments from BigQuery into Firebase? 如何删除 BigQuery 表中的重复记录? - How do you deduplicate records in a BigQuery table? 如何在不到一秒的时间内从 bigquery 表中获取单个行? - How to get individual row from bigquery table less then a second? 如何将一个表中的多行插入到另一个表的单行的结构列中? - How do I insert multiple rows from one table into a struct column of a single row of another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM