select co.id, udf.string_val, udf.id
from customer_order co left join user_def_fields as udf on co.id = udf.document_id
where status = 'h' and order_date between '1/1/2016' and '12/31/2016'
and co.id <> (select document_id from user_def_fields
where (string_val = 'questions' or string_val = 'credit card' or string_val = 'credit hold'
or string_val = 'design'))
co.id | String_val | udf.id
------|------------|--------
9798 | QUESTIONS |UDF-000054
9798 | RUSH ORDER |UDF-000047
9798 | RUSH ORDER |UDF-000024
9799 | RUSH ORDER |UDF-000047
9799 | RUSH ORDER |UDF-000024
9799 | DESIGN |UDF-000054
9801 | RUSH ORDER |UDF-000047
9801 | RUSH ORDER |UDF-000024
9802 | NULL |NULL
9803 | RUSH ORDER |UDF-000047
9803 | RUSH ORDER |UDF-000024
9803 | CHECKED |UDF-000054
Here is a sample of the query results without any filters. Co.id is the order number, the various string_vals are notes within there respective udf fields, udf.id is the field id, and document_id is the order number reference in the udf table. Customer service enters 'Rush Order' in two fields, which is why it shows up twice. Sometimes no notes are entered and the udf fields are null. I really just need the co.id results returned and to be unique. From the above data sample, I want my query to return
co.id
------
9801
9802
9803
My current query gives me an error because it returns multiple results.
AND co.id <> (SELECTshould beAND co.id NOT IN (SELECTif your query returns multiple results