I am trying to build a one to many query in PostgreSQL
that looks at a customer table, a transaction table and the result is a table showing which products the customer has purchased.
table: customers
id
1
2
3
4
5
table: purchases
custid product
1 software 1
1 software 2
3 software 2
3 software 3
4 software 1
5 software 1
5 software 2
5 software 3
The result I want to get is:
custid software1 software2 software3
1 TRUE TRUE NULL
3 NULL TRUE TRUE
4 TRUE NULL NULL
5 TRUE TRUE TRUE
From what I can tell I need to use crosstab
(since postgreSQL
doesn't support pivot
), but I'm not sure on the snytax. I'd also be fine if the output included row:
custid software1 software2 software3
2 NULL NULL NULL
If its easier to do it one way or the other, it doesn't really matter.