简体   繁体   English

如何让购买了 x 产品的客户也购买了 y 产品

[英]How to get the customers who bought x product also bought y product

I'm dealing with multiple datasets.我正在处理多个数据集。

sales(cust, product_no, total_amount)
product(product_no, product_quantity)
X_product_list(X_product_no)
Y_product_list(Y_product_no)

In product table we have two columns product_no, product_quantity.在产品表中,我们有两列 product_no、product_quantity。 product_no contains X_product_no and Y_product_no product_no 包含 X_product_no 和 Y_product_no

Note - Both X_product_list and Y_product_list doesn't match as they are different products.注意 - X_product_list 和 Y_product_list 都不匹配,因为它们是不同的产品。

I'm doing inner join on Sales product_no and Product product_no.我正在对 Sales product_no 和 Product product_no 进行内部连接。 Then trying to check customers who bought X_product_list ie with X_product_no did also bought Y_product_list ie Y_product_no.然后尝试检查购买 X_product_list 即使用 X_product_no 的客户是否也购买了 Y_product_list 即 Y_product_no。 Below is the Snowflake SQL query which i've written.下面是我编写的 Snowflake SQL 查询。 It's returning empty data.它返回空数据。

SELECT 
sales.cust,
FROM sales
INNER JOIN product
      ON sales.product_no = product.product_no
WHERE sales.product_no IN (SELECT X_product_no FROM X_product_list)
      AND sales.product_no IN (SELECT Y_product_no FROM Y_product_list)

I'm trying to get the X_product_no customers who also bought Y_product_no items.我试图让 X_product_no 客户也购买了 Y_product_no 商品。

Using INTERSECT :使用INTERSECT

SELECT sales.cust
FROM sales
JOIN product
  ON sales.product_no = product.product_no
WHERE sales.product_no IN (SELECT X_product_no FROM X_product_list)
INTERSECT
SELECT sales.cust
FROM sales
JOIN product
  ON sales.product_no = product.product_no
WHERE sales.product_no IN (SELECT Y_product_no FROM Y_product_list)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM