I have a table with purchase orders, the orders' lines and a code for each line
| Order_ID | LINE | CODE |
|---|---|---|
| A0001 | 1 | aaaa |
| A0002 | 1 | bbbb |
| A0002 | 2 | xxxx |
| A0003 | 1 | cccc |
| A0004 | 1 | xxxx |
| A0004 | 2 | dddd |
And I need to filter out all the Orders that have at least one line with the code 'xxxx':
| Order_ID | LINE | CODE |
|---|---|---|
| A0001 | 1 | aaaa |
| A0003 | 1 | cccc |
I thought something like this:
SELECT *
FROM MyTable
WHERE Order_ID not in (SELECT * FROM MyTable WHERE CODE = 'xxxx')
BUT, the big problem here is that I'm working with a pretty big query so the subquery is also too large and the whole query takes a lot to run. Is there any workaround to avoid the subquery?