I have the above set of 2 tables and their respective columns.
HEADER
PO_Number (char)
CUSTNMBR (char)
Expiry_Date (datetime)
PO_Status (tinyint, 0 = Open, 1 = Closed)
STATUSDATE (datetime)
BATCH
PORDNMBR (char)
CUSTNMBR (char)
BTCHID (char)
DATE1 (datetime)
In he HEADER table the primary key is (PO_Number, CUSTNMBR). In the BATCH table BTCHID is the primary key. The DATE1 column is the date on which the respective batch was entered. Also, there could be Multiple BTCHIDs entered on different date for each (PO_Number, CUSTNMBR) combination in HEADER table. I have a cutoff date of 2016-07-12 and there are batches before the cutoff date and batches after the cutoff date.
I need to identify those (PO_Number, CUSTNMBR) combination which do not have any batches entered after the cutoff date, where the Expiry_Date form HEADER is < cutoff date.
I wrote the following query initially but the result is bringing records which also have batches after cutoff date. I have verified that using a SELECT * from BATCH query by keying on the PORDNMBR, and in result I can see batches before as well as after cutoff date. Is there any way to identify those which do not have any batches entered after the cutoff date and Expiry_Date form HEADER is < cutoff date?
SELECT BATCH.PORDNMBR, BATCH.CUSTNMBR, BATCH.BTCHID, BATCH.DATE1,
HEADER.PO_Number, HEADER.CUSTNMBR, HEADER.Expiry_Date, HEADER.PO_Status, HEADER.STATUSDATE
FROM BATCH
INNER JOIN
HEADER
ON
BATCH.CUSTNMBR = HEADER.CUSTNMBR
AND
BATCH.PORDNMBR = HEADER.PO_Number
WHERE BATCH.Expiry_Date < '2016-07-12 00:00:00'
AND BATCH.DATE1 < '2016-07-12 00:00:00'