1

我有包含列“CustomerID”和“MONTH”的表 A,以及始终相同的列“PRODUCT”,例如Product X

我也有包含列“CustomerID”和“MONTH”的表 B,但该表包含许多产品,而不仅仅是 X。

我正在尝试从表 B(很多产品)中选择“CustomerID”和“MONTH”与表 A 中的一行匹配的行,以便我最终得到客户在购买产品 X的同一个月购买的产品

我尝试了以下方法:

PROC SQL;
   CREATE TABLE DATA.RESULT AS
   SELECT LOTS.Customer_ID, 
          LOTS.Product_Name, 
          LOTS.Order_Revenue, 
          LOTS.Month_Ordered
   FROM DATA.Family_Holiday_Orders_Plus AS LOTS
   INNER JOIN DATA.X AS X1 ON (X1.Customer_ID = LOTS.Customer_ID)
   INNER JOIN DATA.X AS X2 ON (X2.Month_Ordered = LOTS.Month_Ordered);
QUIT;
4

1 回答 1

1

如果我理解正确,您想选择所有产品,其中存在具有相同和 值的LOTS另一行。DATA.XCustomer_IDMonth_Ordered

SELECT LOTS.Customer_ID, 
       LOTS.Product_Name, 
       LOTS.Order_Revenue, 
       LOTS.Month_Ordered
FROM DATA.Family_Holiday_Orders_Plus AS LOTS
WHERE EXISTS (
    SELECT 1 FROM DATA.X AS X1 WHERE X1.Customer_ID = LOTS.Customer_ID
    AND X1.Month_Ordered = LOTS.Month_Ordered
)
于 2015-04-05T15:31:06.070 回答