I have the following table
| customer_id | id | product_type | serial_number | parent_prod_id |
|---|---|---|---|---|
| 123 | 200 | Camera | 3222333 | 200 |
| 123 | 201 | InstaCam | 3322322 | 200 |
| 123 | 202 | InstaCam | 4332233 | 200 |
| 125 | 200 | Camera | 3222333 | 200 |
| 126 | 200 | Camera | 3222333 | 200 |
My query should return the customer count for each product type but if the same customer purchased a product such as InstaCam which is tied to the parent prod id Camera, then the customer count for the product InstaCam must be 0. In the above table, Camera was purchased by three different customers with customer ids 123, 125 and 126. Since InstaCam was also purchased by one of the customers who purchased the Camera and because the parent_prod_id of InstaCam is the same as the id of Camera, the same customer should not be counted again for the Instacam product so the customer count would be 0.
Expected output:
| serial_number | product_type | customer_count |
|---|---|---|
| 3222333 | Camera | 3 |
| 3322322 | InstaCam | 0 |
| 4332233 | InstaCam | 0 |
I have tried many solutions for hours with no luck. Any help would be greatly appreciated. Thank you.