I am trying to write a query that will select records from a primary table when they meet two criteria conditions in that table and their associated records in the foreign key table match ONLY to one of the three values. The foreign key table can contain up to three records matching back to the one in the primary table. In many situations, one of these three records (in the foreign key table) does contain one of the values I'm looking for, but it also contains records with values that should exclude the record (across all tables) from my result set.
The query below is my best guess as to how to retrieve this result set. The part that's giving problems is in CAPS.
select i.amt, e.code
from (dbo.hExceptionitem ei join dbo.hItem i on (ei.batchnumber = i.batchnumber) and (ei.processingdate = i.processingdate) and (ei.itemrow = i.rownumber) join dbo.hException e on (ei.processingdate = e.processingdate) and (ei.batchnumber = e.batchnumber) and (ei.exceptionrow = e.rownumber))
where i.date='11/1/2006' and (substr(i.num,1,3)='123') and (e.code in ('18','1018','7018')) AND (E.CODE NOT BETWEEN '1' AND '17' AND E.CODE NOT BETWEEN '19' AND '75') AND (E.CODE NOT BETWEEN '1001' AND '1017' AND E.CODE NOT BETWEEN '1019' AND '1075') AND (E.CODE NOT BETWEEN '7001' AND '7017' AND E.CODE NOT BETWEEN '7019' AND '7075')
When I run this query without the part in caps, I do get some records that, legitimately, should not be there. The trouble is, it also returns some records that SHOULD be there that are not returned with the query shown above.... records, that is, that ONLY have an e.code field value of '1018'.
I have tried tons of different permutations of this query. Each time i either wind up missing some records that i know should be there, or i get records in the result set that should not be there.
If anyone has any suggestions, please let me know. Any help is greatly appreciated.
The query below is my best guess as to how to retrieve this result set. The part that's giving problems is in CAPS.
select i.amt, e.code
from (dbo.hExceptionitem ei join dbo.hItem i on (ei.batchnumber = i.batchnumber) and (ei.processingdate = i.processingdate) and (ei.itemrow = i.rownumber) join dbo.hException e on (ei.processingdate = e.processingdate) and (ei.batchnumber = e.batchnumber) and (ei.exceptionrow = e.rownumber))
where i.date='11/1/2006' and (substr(i.num,1,3)='123') and (e.code in ('18','1018','7018')) AND (E.CODE NOT BETWEEN '1' AND '17' AND E.CODE NOT BETWEEN '19' AND '75') AND (E.CODE NOT BETWEEN '1001' AND '1017' AND E.CODE NOT BETWEEN '1019' AND '1075') AND (E.CODE NOT BETWEEN '7001' AND '7017' AND E.CODE NOT BETWEEN '7019' AND '7075')
When I run this query without the part in caps, I do get some records that, legitimately, should not be there. The trouble is, it also returns some records that SHOULD be there that are not returned with the query shown above.... records, that is, that ONLY have an e.code field value of '1018'.
I have tried tons of different permutations of this query. Each time i either wind up missing some records that i know should be there, or i get records in the result set that should not be there.
If anyone has any suggestions, please let me know. Any help is greatly appreciated.