I have a problem I'm trying to resolve. I'd love to get your ideas or help with the method I have in my mind. I created a good analogy for it: I need to tag each record in a table if they're partial purchases or not. It is clearer if you can look at the example below:
I have products classified as Level 2 and Level 1. And each Level 1 product is associated with one or more level 2 products. I only need to check Level 1 purchases, because Level 2 ones can't be partial purchase.
Partial purchase: If a customer who purchased Level 1 product didn't purchase any of the associated Level 2 products, that purchase is partial.
It should be simple, but the columns I have hold the necessary information in a weird way.
Method I have in mind:
For each level 1 purchase
- Get all the Level 2 purchases from that customer with a cte
- Check the product dependency columns of those: if this level 1 product's product ID is in any of those lists(!) it's not a partial purchase. If not it's partial.
This should work, but I can't get it to work in SQL. My question:
- How can I check the each element in each list in the entire column for all the Level 2 purchases from that customer? Because I don't have any way of associating these level 1 & level 2 purchases based on their product dependencies.
Any help is appreciated, I'm a bit slow on SQL What we have is PostgreSQL through Redash