I have a hits table in PostgreSQL that contains a record for each product found in a certain shop.
It looks like this (simplified):
| id | shop | product |
|---|---|---|
| 1 | shop1 | product1 |
| 2 | shop1 | product85 |
| 3 | shop3 | product1 |
| 4 | shop3 | product1 |
| 5 | shop4 | product23 |
| 6 | shop4 | product64 |
| 7 | shop4 | product128 |
As you can see for shop 3, a certain shop may have more than one hit for a certain product.
I want to query it in a way that I get as the output of the query this simple pivot table (which I will use to create a view), where 1 means, the respective shop has the specific product in stock, and 0 means, it doesn't:
| id | shop | product1 | product2 | ... | product23 | ... | product64 | ... | product85 ... |
|---|---|---|---|---|---|---|---|---|---|
| 1 | shop1 | 1 | 0 | ... | 0 | ... | 0 | ... | 1 |
| 2 | shop3 | 1 | 0 | ... | 0 | ... | 0 | ... | 0 |
| 3 | shop4 | 0 | 0 | ... | 1 | ... | 1 | ... | 0 |
Please note: I do not want to hard-code all the columns, as there are many and the number of products might change.
I would prefer not to use crosstab from the tablefunc extension.