Adam12344 Adam12344 - 27 days ago 5
SQL Question

Count product-pairs that have been purchased by same customer

I have a table of customer-product entries for each product a customer has purchased

cust, prod
A,1
A,3
B,1
B,2
B,3


And I want to eventually create a product matrix that has the number of customers that purchased product pairs

1, 2, 3
1, -, 1, 2
2, 1, -, 1
3, 2, 1, -


So, my question is how can I take the 1st table and transform it into something like

prod1, prod2, count
1, 2, 1
1, 3, 2
2, 1, 1
2, 3, 1
3, 1, 2
3, 2, 1

Answer

You have this question answered here:

How to find what products sell well with others in an SQL Server Query

Instead of taking the sale_id, you'll be using the customer

Comments