i want to create a frequency distribution matrix
1.Create a matrix.**Is it possible to get this in separate columns**
customer1 p1 p2 p3
customer 2 p2 p3
customer 3 p2 p3 p1
customer 4 p2 p1
2. Then I have to count the number of products that come together the most
For eg
p2 and p3 comes together 3 times
p1 p3 comes 2 times
p1 p2 comes 2 times
I want to recommend products to customers ,frequency of products that comes together
select customerId,product,count(*) from sales group by customerId,product
Can anyone please help me for a solution to this
I know cursors are not the flavor of the month anymore - but I still find them very useful from time to time
Below code creates a table variable and then loops through the sales data to find out which combination of products are sold together most frequently
This will yield the results:
Interpretation of above results:
Most often, 3 times, a customer only bought coke(s)
Next up, twice a customer bought chips and a coke
Once a customer only bought chips
And once a customer bought a coke & sweets
1.
Array
Multiple columns
2.
This returns the frequency of the whole products' combinations.
In your example
(p1,p2,p3)
is the most frequent (appears twice).(p1,p2)
appears once and so is(p2,p3)
.For frequency of tuples, see @GordonLinoff answer.
If you want pairs of products that customers purchase, then you can use a self join:
You can extend this to more than two products by using more joins.