I have a table with customer ids and product names as columns and I want to create a matrix with the product names so that I can see how many customer buy the same product combinations. I was not able to find a smooth build-in PosgreSQL-function for this use case. And I also didn’t find another way to that. Has someone an idea how I can create such a matrix?
My table:
customerId|productName
----------+-----------
1 | apple
2 | apple
3 | banana
1 | apple
1 | banana
3 | pizza
What I want:
(empty)|apple|banana|pizza
-------+-----+------+------
apple | 2 | 1 | 0
banana | 1 | 2 | 1
pizza | 0 | 1 | 1
2
Answers
I think you want something called
Conditional aggregation
:Result :
Demo here
Do a self-join and aggregate using the filtered count function as the following:
By this join, each bought product (X) for a customer will be joined to all of the bought products for that customer (including the X product itself), now we can easily count the number of customers who bought a combination of two products.
The output for you sample data:
See demo