[英]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![]() |
Apple![]() |
Bundle 1![]() |
Watermelon![]() |
Bundle 2![]() |
Grapes![]() |
Bundle 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![]() |
Apple![]() |
Transactions 1![]() |
Watermelon![]() |
Transactions 2![]() |
Grapes![]() |
Transactions 2![]() |
Lemon![]() |
Transactions 2![]() |
Banana![]() |
Transactions 3![]() |
Pineapple![]() |
Transactions 3![]() |
Kiwi![]() |
Transactions 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.