I am trying to run a query that for a selected person, it will recommend items based on their purchase history and what other people have bought who also bought that item.
Example:
Customer 1 purchases item A, and B.
Customer 2 purchases item B.
Customer 2 is recommended, item A.
I have 4 tables that are used in this I will list them with the relevant row names.
Customer
PK: Cust_ID
Session
PK: Ses_ID
FK: Cust_ID
Order
PK: Order_ID
FK: ItemRef_ID
FK: Session_ID
Item
PK: Item_ID
I’m pretty new to SQL but here’s what I’ve got so far, following that I really have no idea how to go about doing it.
Currently it only gives the items that the selected customer has bought ‘Lani Morgan’, but instead I’d like it to display items that other customers have bought, who have bought the item ‘Lani Morgan’ has bought.
SELECT Item_Desc, Item_ID
FROM rbickers.Item
LEFT JOIN rbickers.Order
ON Item.Item_id = Order.itemref_id
LEFT join rbickers.Session
ON Order.Session_id = Session.Ses_id
left join rbickers.customer
On customer.cust_ID = session.Cust_ID
Where Cust_First = "Lani" and Cust_Last = "Morgan"
GROUP BY Item_Type
LIMIT 10
Any help would really be appreciated, thanks.
So by your basic version and with the supplied schema, the way I see it implemented is by checking for every item, if not bought by customer1, but bought by any other customer buying items customer1 has bought.
Since this schema requires you to join through all of the given tables, I’d ask you to create a view of the join you’ve provided:
and then: