Let's say i have a database with the following structure:
Customers
ID Name
1 Bob
2 Jim
3 Sally
Products
ID Description
10 Pencil
11 Ruler
12 Eraser
Purchases
ID Customer_ID Product_ID
1 1 10
2 1 11
3 2 10
4 3 12
Is it possible (without doing a for each customer, for each product) to write a query to get a format like:
Customer Pencil Ruler Eraser
1 1 1 0
2 1 0 0
3 0 0 1
Here's where I got, but I would like to avoid this structure since it will get lengthy and the number of products will change. Also I am getting duplicate rows when the customer indeed purchased product 10:
SELECT DISTINCT CUSTOMER.CUSTOMER_ID, (case when a.CUSTOMER_ID =
CUSTOMER.CUSTOMER_ID THEN 1 ELSE 0 END) as product10
from
(SELECT PURCHASES.CUSTOMER_ID from PURCHASES
WHERE PURCHASES.PRODUCT_ID = 10) a, CUSTOMER
CUSTOMER_ID product10
1 1
1 0
2 1
2 0
3 0
Thanks!