I have a Student table:
| **Student** | |
|-----------|------------------------------------|
| StudentID | StudentName |
| 1 | Alfreds Futterkiste |
| 2 | Ana Trujillo Emparedados y helados |
| 3 | Antonio Moreno Taquería |
| 4 | Around the Horn |
| 5 | Berglunds snabbköp |
| 6 | Blauer See Delikatessen |
| 7 | Blondel père et fils |
| 8 | Bólido Comidas preparadas |
| 9 | John |
I have a Student Profile table
| **StudentProfile** | | | |
|----------------|------------------------|------|------------|
| StudentID | Profile_Category | Flag | |
| 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 | N | 11/1/2022 |
| 5 | DoNotContact | Y | 10/2/2022 |
| 5 | SubscriptionPlanHolder | Y | 5/1/2022 |
| 6 | DOB | Y | 10/27/2021 |
| 6 | SubscriptionPlanHolder | Y | 11/11/2022 |
| 6 | AddressOnFile | N | 10/1/2022 |
| 9 | DOB | Y | 9/9/2022 |
| 9 | SubscriptionPlanHolder | N | 8/19/2022 |
| 10 | DOB | Y | 11/11/2022 |
| 10 | SubscriptionPlanHolder | Y | 10/1/2022 |
| 10 | AddressOnFile | Y | 9/9/2022 |
| 10 | DoNotContact | Y | 8/19/2022 |
| 11 | DOB | Y | 11/11/2022 |
| 11 | SubscriptionPlanHolder | Y | 10/1/2022 |
| 11 | AddressOnFile | Y | 9/9/2022 |
| 11 | DoNotContact | Y | 8/19/2022 |
Desired output items
- Select all students who got following profile attributes
(Profile_Category = DOB and Flag = 'Y') AND (Profile_Category = SubscriptionPlanHolder and Flag = 'Y')
*In this case I should get StudentID 6, 10, 11
- Select all students who got following profile attributes
(Profile_Category = DOB and Flag = 'Y') AND (Profile_Category = SubscriptionPlanHolder and Flag = 'Y') AND (Profile_Category = AddressOnFile and Flag = 'Y')
*In this case I should get StudentID 10, 11
- Select all students who got following profile attributes
(Profile_Category = DOB and Flag = 'Y') AND (Profile_Category = SubscriptionPlanHolder and Flag = 'Y') AND (Profile_Category = AddressOnFile and Flag = 'Y') AND (Profile_Category = DoNotContact and Flag = 'Y')
*In this case I should get StudentID 10, 11