0

I have two Tables , One Has Products Purchased by each customer and the Other has the Products Purchased by Customers in the same bracket.

I need the Products not purchased by the customer similar to him.

Table 1 - Products Purchased

|Customer_ID  |  Product_Purchased|  
|:-----------:|:-----------------:|  
|1            |  P1               |  
|1            |  P2               |  
|1            |  P3               |  
|2            |  P2               |  
|2            |  P4               |  
|2            |  P5               |  
|3            |  P1               |  

Then There is a table with the Customer ID and the Products Purchased By the Similar Customers

Table 2

|Customer_ID  |  SimilarCustomers_Product_Purchased|  
|:-----------:|:----------------------------------:|  
|1            |  P1                                |  
|1            |  P2                                |  
|1            |  P6                                |  
|1            |  P7                                |  
|1            |  P4                                |  
|2            |  P4                                |  
|2            |  P7                                |  
|2            |  P3                                |  
|2            |  P1                                |  
|2            |  P5                                |  
|2            |  P8                                |  
|3            |  P2                                |  
|4            |  P2                                |  

Now I need the Products that are purchased by similar customers which are not purchased earlier by the actual Ones.

Expected Output Will be Like the one Below:

Output

|Customer_ID  |  Product_Recommended|  
|:-----------:|:-----------------:|  
|1            |  P6               |  
|1            |  P7               |  
|1            |  P4               |  
|2            |  P7               |  
|2            |  P3               |  
|2            |  P1               |  
|2            |  P8               |  
|3            |  P2               |  
|4            |  P2               |  

These are the Products which are not purchased by the customers and are the ones purchased by the similar ones.

I tried "Not Exists" to find the same but it was throwing only the customers that are not in the second table.

Thanks in advance for the help.

Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73

1 Answers1

1

EXCEPT is the straight forward way for such set operations:

select customer_id, product_purchased as product from t1
except
select customer_id, similarcustomers_product_purchased from t2;
Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73