Suppose we have the following tables
Order Header
| Order ID | Status |
|---|---|
| 1 | Open |
| 2 | Open |
| 3 | Complete |
| 4 | Complete |
Order Line Items
| Order Item ID | Order ID | Product ID | Discount Type |
|---|---|---|---|
| 1-1 | 1 | abc | p |
| 1-2 | 1 | def | null |
| 1-3 | 1 | ppp | null |
| 2-1 | 2 | abc | p |
| 2-2 | 2 | def | null |
| 3-1 | 3 | def | null |
| 4-1 | 4 | abc | d |
| 4-2 | 4 | ddd | null |
The original requirement was that, whenever a Order has Order Line Item Product ID = "abc" and Discount Type = "p" then it is mandatory to have Order Line Item Product ID = "ppp". see Order ID 1
Some how in production there are orders where this mandatory product is missing. I want to create a SQL query to fetch such orders. see Order ID 2
Please note
- There can be other Discount Type with other mandatory product see Order ID 4
- There can be other Orders where Order Line Item Product ID = "abc" itself is not present. see Order ID 3