I have a contacts table like this:
Contacts
| CustomerID | CustomerName | Country |
|------------|------------------------------------|---------|
| 1 | Alfreds Futterkiste | Germany |
| 2 | Ana Trujillo Emparedados y helados | Mexico |
| 3 | Antonio Moreno Taquería | Mexico |
| 4 | Around the Horn | UK |
| 5 | Berglunds snabbköp | Sweden |
| 6 | Blauer See Delikatessen | Germany |
| 7 | Blondel père et fils | France |
| 8 | Bólido Comidas preparadas | Spain |
I have another table where all customer'sprofile attributes are saved
Foreignkey is CustomerID
Relationship - one to many ...in profile table, there can be multiple rows for each customer (1 entry /per profile attribute)
CustomerProfile
| CustomerID | exclusion_type | Flag | Effective_Date |
|------------|------------------------|------|----------------|
| 1 | DOB | Y | 11/19/2022 |
| 1 | DoNotContact | Y | 10/25/2022 |
| 3 | AddressOnFile | Y | 9/13/2022 |
| 4 | SubscriptionPlanHolder | Y | 8/8/2022 |
| 5 | DOB | Y | 11/1/2022 |
| 5 | DoNotContact | Y | 10/2/2022 |
| 5 | SubscriptionPlanHolder | Y | 5/1/2022 |
Desired output:
Select all customers from customer table where county is 'Germany' and include customers who got DOB on file and DOB is after Aug 2022 but exclude customers who for DoNotContact flag on their profile