http://sqlfiddle.com/#!18/d1cf0/6
TABLE HEADER
(
[HeaderID] VARCHAR(3),
[description] VARCHAR(50),
[ProcessStatus] bit
);
TABLE DETAILS
(
[HeaderID] VARCHAR(3),
[DETAIL_VALUE_1] VARCHAR(50),
[DETAIL_VALUE_2] VARCHAR(50)
);
SELECT DISTINCT
H.HEADERID
FROM
HEADER H
LEFT JOIN
DETAILS D ON H.HeaderID = D.HeaderID
AND (D.DETAIL_VALUE_1 IS NOT NULL AND
D.DETAIL_VALUE_2 IS NOT NULL)
WHERE H.ProcessStatus = 0
Based on the sample data provided in SQL Fiddle, the above query returns 1,2,3..
I need the query to return only 2,3 as the header 1 has one of the records where DETAIL_VALUE_1 is not null but DETAIL_VALUE_2 is null.
These tables might have million+ records. Header table is the anchor..i.e the Header table will have a flag 'ProcessStatus' to indicate which records have already been processed.
Thanks in advance.